Advertisement
DeepRest

Wiggle Subsequence

Jul 4th, 2022
850
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution:
  2.     def wiggleMaxLength(self, nums: List[int]) -> int:
  3.         n = len(nums)
  4.        
  5.         if n == 1:
  6.             return 1
  7.        
  8.         ans = 1
  9.         prev = nums[0]
  10.        
  11.         for i in range(1, n-1):  
  12.             if (nums[i] > prev and nums[i] > nums[i+1]) or (nums[i] < prev and nums[i] < nums[i+1]):
  13.                 ans += 1  
  14.                 prev = nums[i]
  15.            
  16.         return ans + int(nums[n-1] != prev)
  17.    
Advertisement
RAW Paste Data Copied
Advertisement