-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLongestTurbulentSubarray.java
49 lines (45 loc) · 1.13 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
44
45
46
47
48
49
/*https://leetcode.com/problems/longest-turbulent-subarray/*/
class Solution {
public int maxTurbulenceSize(int[] arr) {
int n = arr.length;
int up = 1;
int down = 1;
int max = 1;
for (int i = 1; i < n; ++i)
{
if (arr[i] > arr[i - 1])
{
up = down + 1;
down = 1;
}
else if (arr[i] < arr[i - 1])
{
down = up + 1;
up = 1;
}
else
down = up = 1;
max = Math.max(max, Math.max(down, up));
}
return max;
}
}
class Solution {
public int maxTurbulenceSize(int[] arr) {
int n = arr.length;
int ans = 1;
int anchor = 0;
for (int i = 1; i < n; ++i)
{
int c = Integer.compare(arr[i-1], arr[i]);
if (c == 0)
anchor = i;
else if (i == n-1 || c * Integer.compare(arr[i], arr[i+1]) != -1)
{
ans = Math.max(ans, i-anchor+1);
anchor = i;
}
}
return ans;
}
}