Sliding Window Maximum
The drill: A fixed-width window slides along an array one step at a time; report the largest value it holds at every stop. Rescanning k elements per stop throws nearly all of its work away — the winning tool is a queue that drops anyone who can never lead again.
An array of integers and a window width k arrive together. Picture a window of that width sliding across the array one position at a time, from the very start until it falls off the end.
At each stop along that slide, the drill wants the largest value currently inside the window. The result is one number per stop, in the same order the window visits them.
The window always holds exactly k elements while it's sliding, and k never exceeds the array's own length, so every stop produces a valid maximum.
- array length can reach into the tens of thousands
- window width k is at least 1 and never exceeds array length
- one maximum reported per window position, left to right
- values may repeat and can be negative
HINT 1 THE NUDGE
Between one stop and the next, only two things change: one value enters on the right, one expires on the left. What knowledge from the last window deserves to survive the slide?
HINT 2 THE STRUCTURE
A value with a newer, bigger-or-equal neighbour to its right can never be a window maximum again — it is dominated for the rest of its life. Keep only the undominated, and notice they form a decreasing lineup.
HINT 3 ONE STEP FROM THE ANSWER
Hold indices in a deque, values decreasing front to back. On arrival, pop the back while it is ≤ the newcomer, then push; pop the front once its index leaves the window. The front is the answer at every stop.
k=2. Keep a deque of indices with values decreasing front to back — the front is always the current window's max.
class Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
dq = collections.deque() # indices; their values run decreasing
out = []
for i, v in enumerate(nums):
while dq and nums[dq[-1]] <= v: # dominated — never a max again
dq.pop()
dq.append(i)
if dq[0] <= i - k: # front slid out of the window
dq.popleft()
if i >= k - 1:
out.append(nums[dq[0]])
return outclass Solution:
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
# rescan all k elements at every stop
return [max(nums[i:i + k]) for i in range(len(nums) - k + 1)]class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] out = new int[n - k + 1];
Deque<Integer> dq = new ArrayDeque<>(); // indices; their values run decreasing
for (int i = 0; i < n; i++) {
while (!dq.isEmpty() && nums[dq.peekLast()] <= nums[i]) {
dq.pollLast(); // dominated — never a max again
}
dq.addLast(i);
if (dq.peekFirst() <= i - k) {
dq.pollFirst(); // front slid out of the window
}
if (i >= k - 1) {
out[i - k + 1] = nums[dq.peekFirst()];
}
}
return out;
}
}class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] out = new int[n - k + 1];
for (int i = 0; i + k <= n; i++) {
int best = nums[i]; // rescan all k elements at every stop
for (int j = i + 1; j < i + k; j++) {
best = Math.max(best, nums[j]);
}
out[i] = best;
}
return out;
}
}✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED