-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestTurbulentSubarray.java
43 lines (40 loc) · 1.3 KB
/
LongestTurbulentSubarray.java
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
package leetcode;
/**
* Created at : 16/09/21
* <p>
* <a href=https://leetcode.com/problems/longest-turbulent-subarray/>978. Longest Turbulent Subarray</a>
*
* @author Himanshu Shekhar
*/
public class LongestTurbulentSubarray {
public int maxTurbulenceSize(int[] arr) {
int n = arr.length;
if (n == 1)
return n;
int ans = 1, count = 1, prev = 0;
for (int i = 0; i < n - 1; i++) {
// current difference
int curr = arr[i + 1] - arr[i];
// skip continuous equal numbers
if (curr == 0) {
count = 1;
prev = 0;
}
// if either we start fresh or there are turbulence in values
// we need to increase count
else if (prev == 0 || prev < 0 && curr > 0 || prev > 0 && curr < 0) {
count++;
prev = curr; // current will become prev in next iteration
ans = Math.max(ans, count); // don't forget to update the answer
}
// in any other case, we need to start fresh
// and reset everything
else {
count = 1;
prev = 0;
i--; // "i" is increasing in every iteration
}
}
return ans;
}
}