16. Sliding Window Maximum
Hard · Array
Given an array of integers and a window size k, find the maximum value in each contiguous subarray of size k as the window slides from left to right across the array.
Return an array of these maximum values, one for each window position. The window starts at the leftmost position and slides one element at a time until it reaches the rightmost position.
Examples
Example 1 Input: nums = [1, 3, 1, 2, 0, 5], k = 3 Output: [3, 3, 2, 5] Explanation: Window [1,3,1] → max 3; [3,1,2] → max 3; [1,2,0] → max 2; [2,0,5] → max 5
Example 2 Input: nums = [1], k = 1 Output: [1] Explanation: Single element array with window size 1 returns that element
Constraints
- Standard input/output constraints apply