Saturday, July 2, 2022

376. Wiggle Subsequence

 一刷 06/2022

Version #1 DP

Time O(N)

Space O(1)

Runtime: 0 ms, faster than 100.00% of Java online submissions for Wiggle Subsequence.
Memory Usage: 41.7 MB, less than 44.24% of Java online submissions for Wiggle Subsequence.

class Solution {

    public int wiggleMaxLength(int[] nums) {

        int maxNegative = 0;

        int maxPositive = 0;

        for (int i = 1; i < nums.length; i++) {

            int diff = nums[i] - nums[i - 1];

            if (diff == 0) {

                continue;

            }

            if (diff < 0) {

                maxNegative = maxPositive + 1;

            } else {

                maxPositive = maxNegative + 1;

            }

        }

        return Math.max(maxPositive, maxNegative) + 1;

    }

}

No comments:

Post a Comment