Skip to content
LC-0376 Medium LeetCode

376. Wiggle Subsequence

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 49% Topics: Array, Dynamic Programming, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def wiggleMaxLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) < 2:
            return len(nums)

        length, up = 1, None

        for i in xrange(1, len(nums)):
            if nums[i - 1] < nums[i] and (up is None or up is False):
                length += 1
                up = True
            elif nums[i - 1] > nums[i] and (up is None or up is True):
                length += 1
                up = False

        return length

Solution from kamyu104/LeetCode-Solutions · MIT