O(n) greedy


  • 0
    Z

    class Solution {
    public:
    int wiggleMaxLength(vector<int>& nums) {
    int n = nums.size();
    if(n <= 1) return n;
    int state = 0;
    int cur = nums[0];
    int len = 1;
    for(int i = 1; i < n; ++i){
    if(nums[i] > nums[i-1]){
    if(state <= 0){
    len++;
    state = 1;
    }
    }
    else if(nums[i] < nums[i-1]){
    if(state >= 0){
    len++;
    state = -1;
    }
    }
    cur = nums[i];
    }
    return len;
    }
    };


Log in to reply
 

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