Skip to content

Commit 67a1978

Browse files
Steve SunSteve Sun
authored andcommitted
add 896
1 parent 7407a07 commit 67a1978

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
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|
2526
|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|
2627
|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|
2728
|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|
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

0 commit comments

Comments
 (0)