Skip to main content

376 - Wiggle Subsequence

Details

KeyValue
Linkhttps://leetcode.com/problems/wiggle-subsequence/
LanguagePython 3
Runtime55 ms, faster than 53.05% of Python3 online submissions for Wiggle Subsequence
Memory Usage13.9 MB, less than 77.55% of Python3 online submissions for Wiggle Subsequence
DatastructuresList[int]
AlgorithmsGreedy w/ Flag
ComplexityTime: O(N) Memory: O(1)

Procedure

  1. ...

Code

class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
if not nums: return 0

length, up = 1, None

for i in range(1, len(nums)):
if nums[i] > nums[i-1] and up != True: length += 1; up = True
if nums[i] < nums[i-1] and up != False: length += 1; up = False
return length