Skip to content
BASA DSA

Sliding Window Maximum

HardSliding Window

Return the maximum value in each contiguous window of length k, from left to right.

Example 1

Input
nums = [2,1,5,3,4]
k = 3
Output
[5,5,5]

Explanation

[2,1,5], [1,5,3] and [5,3,4] each have maximum 5.

Example 2

Input
nums = [-3,-2,-5]
k = 1
Output
[-3,-2,-5]

Explanation

With k=1 each window contains its original value, including negatives.

Example 3

Input
nums = [7,7]
k = 2
Output
[7]

Explanation

The only full-length window has maximum 7; duplicates do not add outputs.

Constraints

  • 1 <= k <= len(nums) <= 100000
  • Values are signed 32-bit integers

Function contractmaxSlidingWindowArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…