Sliding Window Maximum
Master this topic with zero to advance depth.
Sliding Window Maximum
You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
Return the max sliding window.
Visual Representation
nums = [1,3,-1,-3,5,3,6,7], k = 3
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7Examples
Level I: Max-Heap (Lazy Removal)
Intuition
A Max-Heap easily gives us the maximum element. However, when the window slides, removing the element that left the window from the middle of the heap is O(N). Instead, we can use lazy removal: we store (value, index) in the heap. We only remove elements from the top of the heap if their index is outside the current window's bounds.
Thought Process
- Use a Max-Heap storing pairs of
(nums[i], i). - Initialize the heap with the first
kelements. - The max of the first window is the top of the heap.
- Slide the window from
i = ktoN - 1:- Push
(nums[i], i)into the heap. - Check the top of the heap. If its index is
<= i - k, it means it's outside the window. Pop it. - Keep popping until the top is inside the window.
- Record the top's value as the max for the current window.
- Push
Detailed Dry Run
nums = [1,3,-1,-3,5,3], k = 3
- Init k=3: Heap = [(3,1), (1,0), (-1,2)]. Max = 3.
- i=3 (val=-3): Push (-3,3). Heap = [(3,1), (1,0), (-1,2), (-3,3)]. Top idx=1 is inside window (3-3=0, 1>0). Max = 3.
- i=4 (val=5): Push (5,4). Heap = [(5,4), (3,1),...]. Top idx=4 is inside. Max = 5.
- i=5 (val=3): Push (3,5). Heap = [(5,4), ...]. Top idx=4 is inside. Max = 5.
Level II: Balanced BST / Sorted List
Intuition
Maintain a sorted collection of the elements currently in the sliding window. As the window slides, remove the element that is falling out and insert the new element. The maximum is always the last element in the sorted collection.
Detailed Dry Run
nums = [1,3,-1], k = 3
- Initial window [1, 3, -1]. Sorted: [-1, 1, 3]. Max = 3.
- Slide to [3, -1, -3]: Remove 1, Insert -3. Sorted: [-3, -1, 3]. Max = 3.
- Slide to [-1, -3, 5]: Remove 3, Insert 5. Sorted: [-3, -1, 5]. Max = 5.
Level III: Monotonic Deque (Optimal)
Intuition
If we have a descending sequence in the window, say [5, 4, 2], the maximum is 5. If we add a new number 6, then 5, 4, 2 are completely useless because they are smaller than 6 and will leave the window before 6 does! Thus, a monotonically decreasing deque can perfectly track the useful candidates for being the window's max.
Thought Process
- Maintain a deque (double-ended queue) of
indices. - The elements represented by indices in the deque are kept in strictly descending order.
- Loop over
nums[i]from0toN-1:- Maintain Window Bound: If the index at the
frontof the deque is out of the current window (< i - k + 1), remove it from the front. - Maintain Descending Order: While the deque is not empty and the element at the
backis< nums[i], remove it from the back. (They are useless now). - Add current index
ito the back. - Once
i >= k - 1, the window is fully formed. The maximum is always at thefrontof the deque, so appendnums[deque.front()]to the result.
- Maintain Window Bound: If the index at the
Detailed Dry Run
nums = [1,3,-1,-3,5,3], k = 3
- i=0, v=1: dq=[0 (val:1)]
- i=1, v=3: 1 < 3, pop 0. dq=[1 (val:3)]
- i=2, v=-1: -1 < 3, keep. dq=[1, 2]. Window full, max=nums[1]=3
- i=3, v=-3: -3 < -1, keep. dq=[1, 2, 3]. max=nums[1]=3
- i=4, v=5: front 1 out of bounds (1 <= 4-3). pop 1. val 5 > -1, -3. pop 2, 3. push 4. dq=[4 (val:5)]. max=5
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.