Ctrl K

Moving Window Maximum

Hard
A sensor logs the temperature once an hour, producing the array temps. For a report, take a window of width consecutive readings, place it at the start of the array, and shift it right one reading at a time until it reaches the end. For every position of the window, find the highest temperature inside it. Return these values in the order the window moves; there are len(temps) - width + 1 of them.
Example 1
temps=[2, 7, 3, -1, 4, 4, 0], width=3 → [7, 7, 4, 4, 4]

The windows are [2, 7, 3] → 7, [7, 3, -1] → 7, [3, -1, 4] → 4, [-1, 4, 4] → 4, [4, 4, 0] → 4.

Example 2
temps=[-5, -2, -8], width=1 → [-5, -2, -8]

A window of one reading is just that reading.

Example 3
temps=[6, 1, 9, 2], width=4 → [9]

There is a single window covering everything; its maximum is 9.

Constraints
  • 1 ≤ len(temps) ≤ 10⁵
  • -10⁴ ≤ temps[i] ≤ 10⁴
  • 1 ≤ width ≤ len(temps)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Dark Gap Between Street Lamps HardShortest Stretch Reaching a Total HardMaximum Sum With Short Hops HardBest Pair of Close Beacons HardLongest Affordable Run of Machines Hard
loading Python…
Input
temps=[2, 7, 3, -1, 4, 4, 0], width=3
Expected
[7, 7, 4, 4, 4]

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.