27. Sliding Window Maximum
Slide a window of exactly k positions from the beginning of the array to the end. For each possible placement, report the largest value currently inside that window.
Produce one result for each complete window, in left-to-right order. Duplicate maximum values and values leaving the window must be handled correctly.
Example 1
Input
nums=[1,3,-1,-3,5,3,6,7], k=3
Output
[3,3,5,5,6,7]
Explanation: Each output belongs to the next complete length-three interval.
Example 2
Input
nums=[-4,-2,-5], k=2
Output
[-2,-2]
Explanation: All-negative windows still have negative maxima.
Constraints
0 <= nums.length <= 105-231 <= nums[i] <= 231 - 11 <= k <= nums.length- An empty array has no valid window width.
Hints
Hint 1
Can an older smaller value ever beat a newer larger value before the newer one expires?
Hint 2
Keep undominated indices in decreasing value order.
Hint 3
Expire from the front, prune from the back, append right, then read the front.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Expiration and dominated-candidate removal
[1,3,-1,-3,5,3,6,7]
3
[3,3,5,5,6,7]