Sliding Window Maximum

medium
Unit 2 · Graphs I0/3 to milestone

For each window of k consecutive values, report the maximum: peak CPU in each 5-minute window, or the highest price in each week.

Recomputing max per window is O(nk)O(nk). A monotonic deque does it in O(n)O(n): keep indices whose values are decreasing; drop indices that leave the window from the front, and drop smaller values from the back before adding a new one. The front is always the current maximum.

Implement window_max(nums, k) returning a list of len(nums) - k + 1 maxima. Return [] if k > len(nums), and raise ValueError if k < 1.

#deque#sliding-window#monotonic-queue2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Sliding Window Maximum · TechGradify