C++ clear solution, just count how many peaks and valleys.


  • 0
    A
    int wiggleMaxLength(vector<int>& nums) {
        if(nums.size()<=1)return nums.size();
        int result = 1, increase = 0;
        for(int i = 1;i<nums.size();i++){
            if(increase>=0&&nums[i]<nums[i-1]){
                increase = -1;
                result++;
            }
            if(increase<=0&&nums[i]>nums[i-1]){
                increase = 1;
                result++;
            }
        }
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.