Hard
Sliding Window Maximum
- Time limit
- 3 s
- Memory limit
- 256 MB
- Estimated
- 20 min
- Sliding Window
Description
Description
Given an integer array nums and a window size k, consider every contiguous window of k elements as it slides from the start of the array to the end. Return an array of the maximum value in each such window, in the order the windows appear.
Examples
Example 1
- Input:
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3 - Output:
[3, 3, 5, 5, 6, 7]
Example 2
- Input:
nums = [1], k = 1 - Output:
[1]
Example 3
- Input:
nums = [9, 11], k = 2 - Output:
[11]
Expected Function Behavior
Implement maxSlidingWindow(nums, k) and return the array of window maxima.
Constraints
1 <= nums.length <= 100000-1000000000 <= nums[i] <= 10000000001 <= k <= nums.length
Sample cases
3Sample 1
Input
k = 3 nums = [1,3,-1,-3,5,3,6,7]
Expected output
[3,3,5,5,6,7]
Sample 2
Input
k = 1 nums = [1]
Expected output
[1]
Skills and languages
Concepts
- Monotonic Deque
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer