Example 1
ready- Input
nums = [ 1, 3, -1, -3, 5, 3, 6, 7 ] k = 3- Expected
[ 3, 3, 5, 5, 6, 7 ]- Why
- each answer is the largest value in the next three-position window
A monitoring chart rarely cares about one reading in isolation. It asks a moving question: what was the highest value in the most recent block?
Given an integer array nums and a window length k, return the maximum value from every
contiguous window — a block of neighboring elements — of length k, ordered from
left to right.
The first window covers positions 0 through k - 1. Then it moves one position right
at a time until its right edge reaches the end of the array.
nums.length - k + 1 values.1 <= k <= nums.length <= 100_000-100_000 <= nums[i] <= 100_000Hints
For each starting position, scan the next k values and keep their maximum. Now
count how many repeated comparisons happen when two neighboring windows overlap.
If a new value is at least as large as an older value still in the window, the older one can never become a future maximum: it leaves first and loses every comparison.
Store candidate indices in decreasing order of their values. The front gives the maximum, and its index tells you exactly when that candidate has left the window.
How would you expose the same result one value at a time if the readings arrived as a stream and you could retain only O(k) state?
Visible cases
nums = [
1,
3,
-1,
-3,
5,
3,
6,
7
]
k = 3[
3,
3,
5,
5,
6,
7
]nums = [
-4,
2,
2,
9
]
k = 1[
-4,
2,
2,
9
]nums = [
7,
1,
5,
3
]
k = 4[
7
]Interview signal
Move the window one position at a time. At each start, inspect all k values and record
the largest. This is the clean baseline because it follows the task word for word.
def max_sliding_window(nums: list[int], k: int) -> list[int]:
result: list[int] = []
for left in range(len(nums) - k + 1):
best = nums[left]
for index in range(left + 1, left + k):
if nums[index] > best:
best = nums[index]
result.append(best)
return resultNeighboring windows share k - 1 positions, yet this approach compares those values all
over again. Time O(n·k), space O(1) beyond the returned array.
A monotonic deque is a double-ended queue whose stored indices have values in a consistent order. Here, values decrease from front to back, so the front always points to the current maximum.
Before adding index right, do two cleanups:
nums[right]. The new value
outlives them and beats them, so none can become a maximum again.from collections import deque
def max_sliding_window(nums: list[int], k: int) -> list[int]:
candidates: deque[int] = deque()
result: list[int] = []
for right, value in enumerate(nums):
while candidates and candidates[0] <= right - k:
candidates.popleft()
while candidates and nums[candidates[-1]] <= value:
candidates.pop()
candidates.append(right)
if right >= k - 1:
result.append(nums[candidates[0]])
return resultThe deque holds every candidate that could still become a maximum. Its front is best now; everything behind it is smaller but may survive longer. Each index enters once and leaves once, whether from the front or back.
Time O(n), space O(k) — the queue never holds indices outside one window.
This pattern works because deletion is backed by proof: an expired index is unavailable, and an index beaten by a newer value can never win a later window.
Ready to run.
3 cases are queued.
Visible testcase
Case 1
Expected
[ 3, 3, 5, 5, 6, 7 ]