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
上一篇:数据分析学习笔记(三)


下一篇:BigDecimal