Loading practice problems…
Loading your practice workspace…
Loading code editor…
29 / 29
Sign in
Algorithms/Deque

Sliding Window Maximum

hard40 min

Given an integer array and a window size k, return the maximum value in every contiguous window of size k.

Process the array efficiently without scanning every window from scratch.

Example 1

Inputnums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3

Output[3, 3, 5, 5, 6, 7]

Example 2

Inputnums = [1], k = 1

Output[1]

Constraints

  • 1 ≤ k ≤ nums.length ≤ 100,000
  • -10,000 ≤ nums[i] ≤ 10,000
DequeSliding WindowMonotonic Queue