Skip to the code
+50 XP

#272 Peak Load per Time Window

hard

A monitoring dashboard samples server load once a minute into loads. Its "peak" chart shows, for every window of k consecutive minutes, the highest load in that window.

Write windowPeaks(loads, k) that returns the peak of each window, from the window starting at minute 0 to the window ending at the last minute. There are loads.length - k + 1 windows.

Example 1

Input: loads = [2,6,-2,-5,4,1,8,3], k = 3
Output: [6,6,4,4,8,8]

The windows [2, 6, -2], [6, -2, -5], [-2, -5, 4], ... have peaks 6, 6, 4, 4, 8, 8.

Example 2

Input: loads = [1], k = 1
Output: [1]

One window with one minute.

Constraints

  • 1 <= k <= loads.length <= 3 * 10^5
  • -10^4 <= loads[i] <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).