20. Sliding Window Maximum

A window of size k slides from the left of nums to the right, one step at a time. Return the maximum of each window position.

Example 1

Input: nums = [4,2,12,3,8,1,5], k = 3
Output: [12,12,12,8,8]
Explanation: Windows: [4,2,12] [2,12,3] [12,3,8] [3,8,1] [8,1,5].

Example 2

Input: nums = [9,8,7,6], k = 2
Output: [9,8,7]
Hint 1

Keep a deque of indices whose values are in decreasing order.

Hint 2

The front of the deque is always the window maximum.

Sliding WindowMonotonic QueueDeque