Guest User

Untitled

a guest
Jan 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. class Solution(object):
  2. def wiggleMaxLength(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: int
  6. """
  7. if not nums or len(nums) == 1:
  8. return len(nums)
  9. current_num, prev_num = nums[0], nums[0]
  10. count, trend = 0, None
  11. for current_num in nums:
  12. if not trend:
  13. if current_num > prev_num:
  14. trend = "down_to_up"
  15. count += 2
  16. elif current_num < prev_num:
  17. trend = "up_to_down"
  18. count += 2
  19. elif trend == "down_to_up":
  20. if current_num < prev_num:
  21. count += 1
  22. trend = "up_to_down"
  23. elif trend == "up_to_down":
  24. if current_num > prev_num:
  25. prev_num = current_num
  26. count += 1
  27. trend = "down_to_up"
  28. prev_num = current_num
  29. if trend == None:
  30. count = 1
  31. return count
Add Comment
Please, Sign In to add comment