DP short solution, O(n)


  • 0
    	public class Solution {
    	    public int wiggleMaxLength(int[] nums) {
    	        if(nums == null) return 0;
    	    	if(nums.length <= 1) return nums.length;
    	        int n1 = nums[0], n2 = n1;
    	        int res = 1;
    	        for(int n : nums){
    	        	if((n1 == n2 && n != n2) || (n > n2 && n2 < n1) || (n < n2 && n2 > n1)){
            			res++;
    	        		n1 = n2;
    	        	}
    	        	n2 = n;
    	        }
    	        return res;
    	    }
    	}
    

Log in to reply
 

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