Given an integer array `nums` and an integer `k`, there is a sliding window of size `k` that moves from the very left of the array to the very right, one position at a time. Return an array of the maximum value in each window position. For example, given nums = [1,3,-1,-3,5,3,6,7] and k = 3, the windows are: - [1,3,-1] max = 3 - [3,-1,-3] max = 3 - [-1,-3,5] max = 5 - [-3,5,3] max = 5 - [5,3,6] max = 6 - [3,6,7] max = 7 So the answer is [3,3,5,5,6,7].
Example 1
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation: The windows are [1,3,-1], [3,-1,-3], [-1,-3,5], [-3,5,3], [5,3,6] and [3,6,7]. Their maximums are 3, 3, 5, 5, 6 and 7.
Example 2
Input: nums = [1], k = 1
Output: [1]
Explanation: There is a single window, [1], whose maximum is 1.
Example 3
Input: nums = [1,-1], k = 1
Output: [1,-1]
Explanation: With k = 1 each window is one element, so the maximums are the elements themselves.
Maintain a deque of indices in decreasing order of their values. The front of the deque is always the maximum of the current window. When sliding right, remove indices that fell out of the window and remove smaller values from the back.
Run checks all cases above. Submit evaluates all test cases.