1. 程式人生 > >leetcode + DP,up和down。依此遞增

leetcode + DP,up和down。依此遞增

點選開啟連結
//想法比較簡單
class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        if(nums.size() ==0) return 0;
        int n= nums.size();
        vector<int> up(n,1);
        vector<int> down(n,1);
        int i=0;
        for(i=1;i<n;i++){
            if(nums[i]>nums[i-1]){
                up[i] = down[i-1]+1;
                down[i] = down[i-1];
            }
            else if(nums[i] < nums[i-1]){
                down[i] = up[i-1]+1;
                up[i] = up[i-1];
            }
            else{
                up[i] = up[i-1];
                down[i] = down[i-1];
            }
        }
        return max(up[i-1], down[i-1]);
    }
};