Python simple solution


  • 7
    A
    def maxSlidingWindow(self, nums, k):
        ans = []
        queue = []
        for i, v in enumerate(nums):
            if queue and queue[0] <= i - k:
                queue = queue[1:]
            while queue and nums[queue[-1]] < v:
                queue.pop()
            queue.append(i)
            if i + 1 >= k:
                ans.append(nums[queue[0]])
        return ans

  • 1

    I think queue = queue[1:] is a O(k) operation in the worst case, and using collections.deque and popleft method should be a better solution.


Log in to reply
 

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