-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonotonicArray.java
More file actions
35 lines (29 loc) · 1.29 KB
/
MonotonicArray.java
File metadata and controls
35 lines (29 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// An array is monotonic if it is either monotone increasing or monotone decreasing.
// 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].
// Return true if and only if the given array A is monotonic.
// See: https://leetcode.com/problems/monotonic-array/
package leetcode.array_and_hashtable;
public class MonotonicArray {
public boolean isMonotonic(int[] A) {
if (A.length < 3)
return true;
Boolean incr = null;
for (int i = 0; i < A.length - 1; i++) {
if (A[i] < A[i + 1] && incr == null)
incr = true;
else if (A[i] > A[i + 1] && incr == null)
incr = false;
else if (A[i] < A[i + 1] && !incr || A[i] > A[i + 1] && incr)
return false;
}
return true;
}
public static void main(String[] args) {
MonotonicArray sln = new MonotonicArray();
System.out.println(sln.isMonotonic(new int[] { 1, 1, 1, 2 })); // true
System.out.println(sln.isMonotonic(new int[] { 1, 1, 1, 2, 1 })); // false
System.out.println(sln.isMonotonic(new int[] { 2, 2, 2, 1 })); // true
System.out.println(sln.isMonotonic(new int[] { 2, 2, 2, 1, 1, 2 })); // false
}
}