其他分享
首页 > 其他分享> > No.286 - [978] Longest Turbulent Subarray - 简单动态规划

No.286 - [978] Longest Turbulent Subarray - 简单动态规划

作者:互联网

/*
 * @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

标签:arr,lc,int,Turbulent,up,978,down,No.286,ans
来源: https://blog.csdn.net/ShellDawn/article/details/120289559