Simple Java solution Time O(N) Space O(1)


  • 0
    W

    Delete the extra numbers in ascending or descending sequence.

        public int wiggleMaxLength(int[] nums) {
            if (nums.length == 0) return 0;
            int count = 2, pos = 1;
            while(pos < nums.length && nums[pos] == nums[0]) pos++;
            if (pos == nums.length) return 1;
            
            boolean up = nums[pos++] > nums[0];
            for(;pos < nums.length; pos++) {
            	if (nums[pos] == nums[pos - 1] ||
            			(nums[pos] > nums[pos - 1]) == up) continue;
            	count++;
            	up = !up;
            }
            
            return count;
        }
    

Log in to reply
 

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