Wiggle Subsequence(376)

4307 단어 LeetCode1LeetCode1

Greedy - Medium

class Solution:
    def wiggleMaxLength(self, nums: List[int]) -> int:
        if len(nums) == 1 or nums.count(0) == len(nums):
            return 1
        
        answer = 2
        between = []
        
        for i in range(1, len(nums)):
            # The value is not zero
            diff = nums[i] - nums[i - 1]
            if diff > 0:
                between.append(True)
            if diff < 0:
                between.append(False)
        
        for i in range(1, len(between)):
            if between[i] != between[i - 1]:
                answer += 1
        
        return answer

좋은 웹페이지 즐겨찾기