Another O(n) java solution


  • 0
    Q
    public int wiggleMaxLength(int[] nums) {
            int remove = 0;
        	for (int i=1,last=0;i<nums.length;i++){
        	    int curr = nums[i]-nums[i-1];
        		if (curr != 0) {
        		    if (last!=0 && (curr>0)^(last<0))
        		        remove = remove+1;
        		    last = curr;
        		}
        		else
        		    remove++;
        	}
            return nums.length-remove;
        }
    

Log in to reply
 

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