Short non-DP solution, O(1) space, O(n) runtime, 0ms


  • 0
    S

    I found some of the DP solution does not need a memo at all, but one counter for up and down each is enough. Here is my solution.

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

Log in to reply
 

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