File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 1273
1273
βΒ Β βββ Solution.java
1274
1274
βββ 0892.Surface Area of 3D Shapes
1275
1275
βΒ Β βββ Solution.java
1276
+ βββ 0896.Monotonic Array
1277
+ βΒ Β βββ Solution.js
1276
1278
βββ 0898.Bitwise ORs of Subarrays
1277
1279
βΒ Β βββ Solution.java
1278
1280
βββ 0905.Sort Array By Parity
You canβt perform that action at this time.
0 commit comments