-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path1.java
33 lines (31 loc) · 793 Bytes
/
1.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
class Solution {
public int maxTurbulenceSize(int[] A) {
if (A.length <= 1) {
return A.length;
}
int N = A.length;
// 定义两个 DP 数组 f, g
int[] f = new int[N+1];
int[] g = new int[N+1];
f[0] = 0;
g[0] = 0;
f[1] = 1;
g[1] = 1;
int res = 1;
for (int k = 2; k <= N; k++) {
if (A[k-2] < A[k-1]) {
f[k] = g[k-1] + 1;
g[k] = 1;
} else if (A[k-2] > A[k-1]) {
f[k] = 1;
g[k] = f[k-1] + 1;
} else {
f[k] = 1;
g[k] = 1;
}
res = Math.max(res, f[k]);
res = Math.max(res, g[k]);
}
return res;
}
}