Short and Simple Solution


  • 0
    int wiggleMaxLength(vector<int>& nums) {
        if (nums.empty()) {
            return 0;
        }
        int up = nums[0];
        int down = nums[0];
        int count = 1;
        for (int i = 1; i < nums.size(); ++i) {
            if (nums[i] > nums[i-1]) {
                count += down == nums[i-1] ? 1 : 0;
                up = nums[i];
            } else if (nums[i] < nums[i-1]) {
                count += up == nums[i-1] ? 1 : 0;
                down = nums[i];
            }
        }
        return count;
    }
    

Log in to reply
 

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