Sliding Window Maximum

A window of width k slides over nums from the left, one position at a time, until it reaches the right end. Return the largest number inside the window at each of its n - k + 1 positions.

Constraints

  • 1 ≤ k ≤ length of nums ≤ 100,000
  • -10,000 ≤ nums[i] ≤ 10,000

Example 1

Input
nums = [1,3,-1,-3,5,3,6,7]
k = 3
Output
[3,3,5,5,6,7]
Why:

The window [1, 3, -1] has 3 as its largest, then [3, -1, -3] has 3, and so on.

Example 2

Input
nums = [4]
k = 1
Output
[4]
Why:

One window holding the one number.

Hints

    Was this a good challenge?

    Solved by
    0
    Submissions
    0
    Accepted
    –