DP Python Solution (beat over 98.5%)


  • 0
    J

    every change of current difference( eg. up edge -> down edge or down edge -> up edge) add result by 1

    def wiggleMaxLength(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            
            
            if(not nums):
                return 0
            length = len(nums)
            if(length == 1):
                return 1
            x1 = nums[0]
            res = 1
            dif = 0
            for i in xrange(length - 1):
                x2 = nums[i + 1]
                d = x2 - x1
                x1 = x2
                if(d == 0):
                    continue
                elif(d > 0):
                    if(dif <= 0):
                        res += 1
                        dif = 1
                    continue
                else:
                    if(dif >= 0):
                        res += 1
                        dif = -1
                        
            return res
    

Log in to reply
 

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