File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
22
22
23
23
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
24
24
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25
+ | 896| [ Monotonic Array] ( https://leetcode.com/problems/monotonic-array/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_896.java ) | O(n) | O(1) | | Easy|
25
26
| 844| [ Backspace String Compare] ( https://leetcode.com/problems/backspace-string-compare/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_844.java ) | O(n) | O(1) | | Easy|
26
27
| 832| [ Flipping an Image] ( https://leetcode.com/problems/flipping-an-image/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_832.java ) | O(n) | O(1) | | Easy|
27
28
| 830| [ Positions of Large Groups] ( https://leetcode.com/problems/positions-of-large-groups/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_830.java ) | O(n) | O(n) | | Easy|
Original file line number Diff line number Diff line change
1
+ package com .fishercoder .solutions ;
2
+
3
+ /**
4
+ * 896. Monotonic Array
5
+ *
6
+ * An array is monotonic if it is either monotone increasing or monotone decreasing.
7
+ *
8
+ * An array A is monotone increasing if for all i <= j, A[i] <= A[j].
9
+ * An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
10
+ *
11
+ * Return true if and only if the given array A is monotonic.
12
+ *
13
+ * Example 1:
14
+ *
15
+ * Input: [1,2,2,3]
16
+ * Output: true
17
+ * Example 2:
18
+ *
19
+ * Input: [6,5,4,4]
20
+ * Output: true
21
+ * Example 3:
22
+ *
23
+ * Input: [1,3,2]
24
+ * Output: false
25
+ * Example 4:
26
+ *
27
+ * Input: [1,2,4,5]
28
+ * Output: true
29
+ * Example 5:
30
+ *
31
+ * Input: [1,1,1]
32
+ * Output: true
33
+ *
34
+ *
35
+ * Note:
36
+ *
37
+ * 1 <= A.length <= 50000
38
+ * -100000 <= A[i] <= 100000
39
+ */
40
+ public class _896 {
41
+ public static class Solution1 {
42
+ public boolean isMonotonic (int [] A ) {
43
+ int i = 0 ;
44
+ for (; i < A .length - 1 ; i ++) {
45
+ if (A [i ] <= A [i +1 ]) {
46
+ continue ;
47
+ } else {
48
+ break ;
49
+ }
50
+ }
51
+ if (i == A .length - 1 ) {
52
+ return true ;
53
+ }
54
+ i = 0 ;
55
+ for (; i < A .length - 1 ; i ++) {
56
+ if (A [i ] >= A [i +1 ]) {
57
+ continue ;
58
+ } else {
59
+ break ;
60
+ }
61
+ }
62
+ return i == A .length - 1 ;
63
+ }
64
+ }
65
+
66
+ public static void main (String ... args ) {
67
+ int [] A = new int []{1 , 3 , 2 };
68
+ Solution1 solution1 = new Solution1 ();
69
+ System .out .println (solution1 .isMonotonic (A ));
70
+ }
71
+ }
You can’t perform that action at this time.
0 commit comments