Java O(n) solution. You only need to count the peak and valley


  • 0
    H
    public class Solution {
        public int wiggleMaxLength(int[] nums) {
            if(nums == null || nums.length == 0)
                return 0;
            if(nums.length == 1)
                return 1;
            int count = 1;
            int i = 0, j = i + 1;
            while(i < nums.length && j < nums.length){
                if(nums[j] == nums[i]){
                    ++i;
                    ++j;
                }else if(nums[j] > nums[i]){
                    ++count;
                    int k = j + 1;
                    while(k < nums.length && nums[k] >= nums[k - 1]){
                        ++k;
                    }
                    if(k == nums.length)
                        return count;
                    i = k - 1;
                    j = k;
                }else{
                    ++count;
                    int k = j + 1;
                    while(k < nums.length && nums[k] <= nums[k - 1]){
                        ++k;
                    }
                    if(k == nums.length)
                        return count;
                    i = k - 1;
                    j = k;
                }
            }
            return count;
        }
    }

Log in to reply
 

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