Hardarraysdeque
Sliding Window Maximum
Report the largest value in every window of fixed width as it slides.
A window of width k slides across nums one position at a time. Return the maximum inside the window at every position, left to right.
Constraints
- 1 ≤ nums.length ≤ 100,000
- 1 ≤ k ≤ nums.length
- -10⁹ ≤ nums[i] ≤ 10⁹
Rescanning each window is too slow!
Examples
- Input
- nums = [1,3,-1,-3,5,3,6,7], k = 3
- Output
- [3,3,5,5,6,7]
- Input
- nums = [1], k = 1
- Output
- [1]
- Input
- nums = [9,8,7,6], k = 2
- Output
- [9,8,7]
- Why
- A decreasing array: each window's first value wins.
Submitting also runs 6 hidden tests.
Limits
2000 ms and 256 MB per test case.