Skip to content

Commit 540318b

Browse files
authored
Create Longest Strictly Increasing or Strictly Decreasing Subarray.java
1 parent d8afeb2 commit 540318b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int longestMonotonicSubarray(int[] nums) {
3+
int max = 1;
4+
int idx = 0;
5+
int n = nums.length;
6+
while (idx < n) {
7+
if (idx == n - 1 || nums[idx + 1] == nums[idx]) {
8+
idx++;
9+
continue;
10+
}
11+
boolean increase = nums[idx + 1] > nums[idx];
12+
int start = idx;
13+
while (idx + 1 < n && (
14+
(increase && nums[idx + 1] > nums[idx]) ||
15+
(!increase && nums[idx + 1] < nums[idx]))) {
16+
idx++;
17+
}
18+
max = Math.max(max, idx - start + 1);
19+
}
20+
return max;
21+
}
22+
}

0 commit comments

Comments
 (0)