Skip to content

Commit 02f3fc8

Browse files
authored
Merge pull request doocs#249 from Gapur/master
896. Monotonic Array
2 parents 294dc15 + 7912a57 commit 02f3fc8

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Monotonic Array
2+
3+
An array is monotonic if it is either monotone increasing or monotone decreasing.
4+
5+
An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
6+
7+
Return true if and only if the given array A is monotonic.
8+
9+
## Example 1:
10+
```
11+
Input: [1,2,2,3]
12+
Output: true
13+
```
14+
15+
## Example 2:
16+
```
17+
Input: [6,5,4,4]
18+
Output: true
19+
```
20+
21+
## Example 3:
22+
```
23+
Input: [1,3,2]
24+
Output: false
25+
```
26+
27+
## Example 4:
28+
```
29+
Input: [1,2,4,5]
30+
Output: true
31+
```
32+
33+
## Example 5:
34+
```
35+
Input: [1,1,1]
36+
Output: true
37+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var isMonotonic = function(A) {
2+
var check = A[0] <= A[A.length - 1]
3+
? (a1, a2) => a1 <= a2
4+
: (a1, a2) => a1 >= a2;
5+
for (var i = 0; i < A.length - 1; i++) {
6+
if (!check(A[i], A[i + 1])) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
};

β€Žsolution/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,8 @@
12731273
β”‚Β Β  └── Solution.java
12741274
β”œβ”€β”€ 0892.Surface Area of 3D Shapes
12751275
β”‚Β Β  └── Solution.java
1276+
β”œβ”€β”€ 0896.Monotonic Array
1277+
β”‚Β Β  └── Solution.js
12761278
β”œβ”€β”€ 0898.Bitwise ORs of Subarrays
12771279
β”‚Β Β  └── Solution.java
12781280
β”œβ”€β”€ 0905.Sort Array By Parity

0 commit comments

Comments
Β (0)