Description
Submission
class Solution {
int dp[2][40005]; // max til now(continuously)
public:
int maxTurbulenceSize(vector<int>& arr) {
dp[0][0] = dp[1][0] = 1;
for(int i = 1; i < arr.size(); ++i) {
if(i % 2 == 1) { // if k is odd
if(arr[i-1] < arr[i]) {
dp[0][i] = dp[0][i-1] + 1;
} else {
dp[0][i] = 1;
}
if(arr[i-1] > arr[i]) {
dp[1][i] = dp[1][i-1] + 1;
} else {
dp[1][i] = 1;
}
} else {
if(arr[i-1] < arr[i]) {
dp[1][i] = dp[1][i-1] + 1;
} else {
dp[1][i] = 1;
}
if(arr[i-1] > arr[i]) {
dp[0][i] = dp[0][i-1] + 1;
} else {
dp[0][i] = 1;
}
}
}
int ret = 0;
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < arr.size(); ++j) {
ret = max(ret, dp[i][j]);
// cout << dp[i][j] << " ";
}
// cout << endl;
}
return ret;
}
};