Skip to content
BytePatterns

Window Maximums

HardArrays#sliding-window#monotonic-deque~40m

Problem

A window of fixed width k slides across a list of integers, one position at a time, from the far left to the far right. Report the maximum value inside the window at every stop. The output therefore holds one number for each window position.

Examples

Input:  nums = [1, 4, 2, 7, 3, 3], k = 3
Output: [4, 7, 7, 7]
Why:    the windows are 1,4,2 then 4,2,7 then 2,7,3 then 7,3,3
Input:  nums = [5, 5, 5], k = 1
Output: [5, 5, 5]
Why:    edge case, a width of one makes every element its own maximum
Input:  nums = [2, 1], k = 2
Output: [2]
Why:    the window covers the whole list, so there is a single stop

Hints

0 / 3

Stuck on the idea rather than the code? Monotonic Stack covers it.