Easy understaning DP solution with O(n) in Python


  • 0
    K
        def wiggleMaxLength(self, nums):
            if len(nums) < 2:
                return len(nums)
            up = [1] * len(nums)
            down = [1] * len(nums)
            for i in range(1, len(nums)):
                if nums[i] > nums[i-1]:
                    up[i] = down[i-1] + 1
                    down[i] = down[i-1]
                elif nums[i] < nums[i-1]:
                    down[i] = up[i-1] + 1
                    up[i] = up[i-1]
                else:
                    down[i] = down[i-1]
                    up[i] = up[i-1]
            return max(down[-1], up[-1])
    
    

Log in to reply
 

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