Skip to the code
cf.
+50 XP

#387 Busiest Moment per Window

hard

A server dashboard plots the peak CPU load over a sliding window: for every run of w consecutive samples, it shows the largest one.

Write windowPeaks(loads, w) that returns an array with the maximum of loads[i..i + w - 1] for every start i from 0 to loads.length - w, in order. w is between 1 and loads.length.

Example 1

Input: loads = [4,2,12,3,8,1,5], w = 3
Output: [12,12,12,8,8]

The windows are [4, 2, 12], [2, 12, 3], [12, 3, 8], [3, 8, 1], and [8, 1, 5].

Example 2

Input: loads = [7,3], w = 1
Output: [7,3]

With w = 1, each sample is its own window.

Constraints

  • 1 <= w <= loads.length <= 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 solves the problem when they all pass.

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