Sliding Window Maximum

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 19. Everything before it counts as cleared.

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