Learn
Practice
Newsletter
Resources
Mobile
New
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Sliding Window Maximum
Bookmark
Brute Force
Max-Heap
Monotonic Deque
Input
Example 1
Single Element
k = 1
Example 2
Custom
nums
=
[1, 3, -1, -3, 5, 3, 6, 7]
,
k
=
3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
result
scan each window of size 3 for its maximum
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
result
scan each window of size 3 for its maximum
max =
3
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
scan
result
max = max(…, nums[1]) =
3
max =
3
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
scan
result
3
max = max(…, nums[1]) =
3
max =
3
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
result
3
3
window max =
3
→ result
max =
5
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
scan
result
3
3
max = max(…, nums[4]) =
5
max =
-3
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
scan
result
3
3
5
max = max(…, nums[3]) =
-3
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
result
3
3
5
5
window
[4..6]
— scan for the max
max =
6
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
scan
result
3
3
5
5
max = max(…, nums[6]) =
6
max =
6
window 3
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
scan
result
3
3
5
5
6
max = max(…, nums[6]) =
6
1
3
-1
-3
5
3
6
7
0
1
2
3
4
5
6
7
result
3
3
5
5
6
7
result =
[3, 3, 5, 5, 6, 7]
algo
master
.
io
Step:
Brute force: scan each window of size 3 for its maximum.
0 / 31
Input
Example 1
Single Element
k = 1
Example 2
Custom
nums
=
[1, 3, -1, -3, 5, 3, 6, 7]
,
k
=
3
0 / 31
algo
master
.
io
Step:
Brute force: scan each window of size 3 for its maximum.