Easy To Understand Python Code


  • 0
    L
    def wiggleMaxLength(self, nums):
        # remove all the adjacent duplications
        idx = 1
        while idx < len(nums):
            if idx < len(nums) and nums[idx] == nums[idx-1]:
                del nums[idx]
            else:
                idx += 1
        
        # consider special cases
        if len(nums) <= 1:
            return len(nums)
        
        # count the peaks otherwise
        peaks = 0
        for idx in range(1,len(nums)-1):
            if (nums[idx]-nums[idx-1])*(nums[idx]-nums[idx+1])>0:
                peaks += 1
        return peaks+2

Log in to reply
 

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