C++ O(n) solution with O(1) space,not dp


  • 0
    J
    int wiggleMaxLength(vector<int>& nums) {
            int flag = 0,m = nums.size(),res = 1,temp;
            if(m == 0) return 0;
            for(int i = 1;i != m;++i){
                if(flag == 0)
                    flag = (nums[i-1] - nums[i]);
                temp = flag *(nums[i] - nums[i-1]);
                flag = temp >= 0?flag:0-flag;
                res = temp >= 0?res:res+1;
            }
            return res;
        }

Log in to reply
 

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