Basic example
Input
nums = [1,3,-1,-3,5,3,6,7], k = 3
Output
[3, 3, 5, 5, 6, 7]
Windows and their maximums: [1,3,-1]->3, [3,-1,-3]->3, [-1,-3,5]->5, [-3,5,3]->5, [5,3,6]->6, [3,6,7]->7.
Full lesson preview
Compute the maximum value in each sliding window of size k using a deque for O(n) time.
Problem statement
Task
Examples
Input
nums = [1,3,-1,-3,5,3,6,7], k = 3
Output
[3, 3, 5, 5, 6, 7]
Windows and their maximums: [1,3,-1]->3, [3,-1,-3]->3, [-1,-3,5]->5, [-3,5,3]->5, [5,3,6]->6, [3,6,7]->7.
Input format
Output format
Constraints
Samples
Input
nums = [4,3,2,1], k = 1
Output
[4, 3, 2, 1]
Each window of size 1 has its only element as the maximum.