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
| class Solution { public int maxTurbulenceSize(int[] A) { int n = A.length; int[][] dp = new int[n+1][2]; dp[1][0] = 1; dp[1][1] = 1;
for (int i = 2; i <= n; i++) { if (A[i - 2] < A[i - 1]) { dp[i][1] = dp[i - 1][0] + 1;
dp[i][0] = 1; }else if(A[i - 2] > A[i - 1]){ dp[i][0] = dp[i - 1][1] + 1;
dp[i][1] = 1; }else { dp[i][0] = 1; dp[i][1] = 1; } }
int ans = Integer.MIN_VALUE; for (int i = 1; i <= n; i++) { ans = Math.max(ans, Math.max(dp[i][0], dp[i][1])); }
return ans; } }
|