Really Concise 7-line Java Solution With O(1) Space and O(n) Time


  • 0
    G
    public int wiggleMaxLength(int[] nums) {
            if(nums.length == 0) return 0;
            int up = 1, down = 1;
            for (int i = 1; i < nums.length; ++i) {
                if (nums[i] > nums[i - 1]) up = down + 1;
                if (nums[i] < nums[i - 1]) down = up + 1;
            }
            return Math.max(down, up);
        }

Log in to reply
 

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