← DiffPush

Sliding Window Maximum

DiffPush Tier Stack and Queues · Implementation O(n) · O(k)

The Web Farm Load Sampler

An ops dashboard samples a server farm's request counters once per second and reports the peak load over every rolling 10-second window. Recomputing each window from scratch would double-count nearly everything, so the sampler keeps a shortlist of only the candidates that could still become a window peak — evicting any older sample a newer one has overshadowed.

Input: An integer array nums and a window size k.

Output: Return the maximum of each window as it slides from left to right.

Constraints

Examples

Example 1

Input: {"nums":[1,3,-1,-3,5,3,6,7],"k":3}
Output: [3,3,5,5,6,7]
Each of the six windows reports its own peak as the frame advances one step at a time.

Example 2

Input: {"nums":[9,8,7,6],"k":2}
Output: [9,8,7]
A strictly falling sequence: each window's peak is simply its first element.

Solve this in your browser →

Also on LeetCode ↗