Easy to understand Python, one pass solution


  • 0
    class Solution(object):
        def wiggleMaxLength(self, nums):
            n = len(nums)
            if n == 0: return 0
            m = 0; prevDiff = 0
            for i in range(n-1):
                diff = nums[i+1] - nums[i]
                if diff*prevDiff < 0: m += 1
                elif diff != 0 and m == 0: m = 1
                if diff != 0: prevDiff = diff
            return m+1
    

Log in to reply
 

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