Elegant Python Code using stack (Deque)


  • 0
    H

    class Solution(object):
    def wiggleMaxLength(self, nums):
    q = collections.deque()
    for num in nums:
    if len(q) == 0:
    q.append(num)
    continue
    if num == q[-1]:
    continue
    # if wrong sequence appears, pop out middle value
    if len(q) >= 2 and (num > q[-1] > q[-2] or num < q[-1] < q[-2]):
    q.pop()
    q.append(num)
    return len(q)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.