153. Leetcode 376. 摆动序列 (贪心算法-进阶题目)

153. Leetcode 376. 摆动序列 (贪心算法-进阶题目)_第1张图片

class Solution:
    def wiggleMaxLength(self, nums: List[int]) -> int:
        n = len(nums)
        if n < 2:
            return n
        
        prediff = nums[1] - nums[0]
        ret = (2 if prediff!=0 else 1)
        for i in range(2, n):
            diff = nums[i] - nums[i-1]
            if (prediff >=0 and diff <0) or (prediff <=0 and diff>0):
                ret += 1
                prediff = diff

        return ret

 

你可能感兴趣的:(2022刷题-目标400+,leetcode,贪心算法,算法)