Python simple solution

  • 7
    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:
            if i + 1 >= k:
        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.