Super easy O(n)time, O(1) space JAVA solution, no signs of differences needed


  • 0
    J
     public int wiggleMaxLength(int[] nums) {
            if(nums == null || nums.length == 0) return 0;
            if(nums.length == 1) return 1;
            int result = 1;
            int i = 1;
            while(i < nums.length){
                if(nums[i] == nums[i-1]){
                    i++;
                } 
                else if (nums[i] > nums[i-1]){
                    result ++;
                    i++;
                    while(i < nums.length && nums[i] >= nums[i-1]){
                        i++;
                    }
                }
                else{
                    result ++;
                    i++;
                    while(i < nums.length && nums[i] <= nums[i-1]){
                        i++;
                    }
                }
            }
            return result;
            
        }

Log in to reply
 

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