#239
Sliding Window Maximum
hard· Sliding Windowruns: 0You are given an array of integers nums and a sliding window of size k which moves from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max for each window.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 436
from collections import deque
class Solution:
def maxSlidingWindow(self, nums: list[int], k: int) -> list[int]:
result = []
q = deque()
for i, num in enumerate(nums):
while q and nums[q[-1]] < num:
q.pop()
q.append(i)
if q[0] == i - k:
q.popleft()
if i >= k - 1:
result.append(nums[q[0]])
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.