/*
* @lc app=leetcode id=978 lang=cpp
*
* [978] Longest Turbulent Subarray
*/
// @lc code=start
class Solution {
public:
int maxTurbulenceSize(vector<int>& arr) {
int N = arr.size();
if(N<=1){
return N;
}
int up = 1, down = 1;
int ans = 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{
up = down = 1;
}
ans = max(ans, max(up, down));
}
return ans;
}
};
// @lc code=end
No.286 - LeetCode[978] Longest Turbulent Subarray - 简单动态规划
最新推荐文章于 2024-10-01 18:52:56 发布