Highest in Every Window

HardSliding Window≈ 45 min

You are given a list of integers nums and a window size k. A window of k numbers in a row slides along the list one place at a time, from the start to the end. Return the highest number in every window, in order.

There are len(nums) − k + 1 windows, so the answer holds that many numbers.

Examples

text
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3   →  [3, 3, 5, 5, 6, 7]
nums = [4], k = 1                          →  [4]
nums = [9, 8, 7, 6], k = 2                 →  [9, 8, 7]
nums = [2, 2, 2, 2], k = 3                 →  [2, 2]

In the first example the windows are [1, 3, -1], [3, -1, -3], [-1, -3, 5], [-3, 5, 3], [5, 3, 6] and [3, 6, 7].

Constraints

  • Between 1 and 20,000 numbers, each between -10,000 and 10,000; k is between 1 and the length of the list.
  • Aim for O(n): finding each window's highest from scratch costs n × k.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.