Python O(n) greedy algorithm


  • 0
    P
    class Solution(object):
        def wiggleMaxLength(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            ret = len(nums)
            prevDiff = None
            signBit = 1 << 63
            for i in xrange(1, len(nums)):
                curDiff = nums[i] - nums[i - 1]
                if curDiff == 0 or (prevDiff and (prevDiff & signBit) == (curDiff & signBit)):
                    ret -= 1
                else:
                    prevDiff = curDiff
            return ret
    

Log in to reply
 

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