-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonotonicArray.java
More file actions
52 lines (50 loc) · 1.76 KB
/
MonotonicArray.java
File metadata and controls
52 lines (50 loc) · 1.76 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Solution for 896. Monotonic Array: https://leetcode.com/problems/monotonic-array/
*/
public class Solution {
/**
* Sequentially determines whether the given array is monotonically decreasing or increasing.
* Time Complexity: O(N), Space Complexity: O(1)
*
* @param nums the array of numbers to test for being monotonic
* @return whether the given array is monotonically increasing or decreasing
*/
public boolean isMonotonic(int[] nums) {
boolean inc = true;
boolean dec = true;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] >= nums[i + 1]) {
inc = false;
} else if (nums[i] <= nums[i + 1]) {
dec = false;
}
}
return inc || dec;
}
/**
* Solution 2. Compare the first and last elements to see whether the array needs to monotonically decrease or
* increase. This approach does not work if nums is a stream, since the first and last elements can't be compared.
* Time Complexity: O(N), Space Complexity: O(1)
*
* @param nums the array of numbers to test for being monotonic
* @return whether the given array is monotonically increasing or decreasing
*/
public boolean isMonotonicTwo(int[] nums) {
boolean dec = nums[0] <= nums[nums.length - 1];
if (dec) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
return false;
}
}
return true;
} else {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] < nums[i + 1]) {
return false;
}
}
return true;
}
}
}