Share My Python Solution


  • 0
    M
    • If length of nums <= 2, then return length.
    • top represents the last relation between nums[i] and nums[i - 1].
    • Time Complexity: O(n), where n is length of nums
    class Solution(object):
        def wiggleMaxLength(self, nums):
            length = len(nums)
            if length <= 2: return length
            
            top = nums[1] > nums[0]
            res = 1
            
            for i in xrange(2, length):
                if nums[i] == nums[i - 1]:
                    continue
                tmp = nums[i] > nums[i - 1]
                if top ^ tmp:
                    res += 1
                    top = tmp
            return res + 1
    

Log in to reply
 

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