Skip to the code
+50 XP

#134 Peak Load in Every Window

hard

A monitoring chart shows CPU load samples in loads. Each point of its "peak" line is the highest load among k samples in a row.

Write windowPeaks(loads, k) that returns the maximum of every window of k consecutive samples, from the first window to the last. k is never more than loads.length. Aim for O(n) time, so the answer stays fast for big k.

Example 1

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

For example the window [-2, -5, 4] peaks at 4.

Example 2

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

With k = 1 each sample is its own window.

Constraints

  • 1 <= k <= loads.length <= 200000
  • -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).