Python O(n) Concice Solution


  • 1
    D

    Inspiration from @yuleleegerlee

    class Solution(object):
        def wiggleMaxLength(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums)
            if n < 2: return n
            if nums[1] > nums[0]: inc, dec = 1, 0
            elif nums[1] < nums[0]: inc, dec = 0, 1
            else: inc, dec = 0, 0
            
            for i in range(2, n):
                if nums[i] > nums[i-1]:
                    inc = dec + 1
                if nums[i] < nums[i-1]:
                    dec = inc + 1
            return max(inc, dec) + 1

Log in to reply
 

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