MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / maxSlidingWindow

Method maxSlidingWindow

python/0239-sliding-window-maximum.py:2–22  ·  view source on GitHub ↗
(self, nums: List[int], k: int)

Source from the content-addressed store, hash-verified

1class Solution:
2 def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
3 output = []
4 q = collections.deque() # index
5 l = r = 0
6 # O(n) O(n)
7 while r < len(nums):
8 # pop smaller values from q
9 while q and nums[q[-1]] < nums[r]:
10 q.pop()
11 q.append(r)
12
13 # remove left val from window
14 if l > q[0]:
15 q.popleft()
16
17 if (r + 1) >= k:
18 output.append(nums[q[0]])
19 l += 1
20 r += 1
21
22 return output

Callers

nothing calls this directly

Calls 2

popleftMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected