Given an integer array nums and a window length k, slide a window of exactly k consecutive elements from the left end of the array to the right end, one position at a time. Return the maximum value inside the window at each position, in order from left to right.
Function Signature
def window_maxima(nums: list[int], k: int) -> list[int]:
Rules
-
The window that starts at index
i
covers
nums[i]
through
nums[i + k - 1]
.
-
The output has exactly
len(nums) - k + 1
values; the value at index
i
is the maximum of the window that starts at index
i
.
-
Equal values are fine: the output holds values, not positions.
Constraints
-
1 <= len(nums) <= 10^5
-
1 <= k <= len(nums)
-
-10^9 <= nums[i] <= 10^9
, so every value fits in a 32-bit signed integer.
Examples
Example 1
Input: nums = [4, 2, 12, 3, 8, 1, 7], k = 3
Output: [12, 12, 12, 8, 8]
The windows are [4, 2, 12], [2, 12, 3], [12, 3, 8], [3, 8, 1] and [8, 1, 7].
Example 2
Input: nums = [5, -1, -3, 2], k = 1
Output: [5, -1, -3, 2]
Each window holds one element, so the output equals the input.
Example 3
Input: nums = [-2, -5, -1], k = 3
Output: [-1]
There is a single window, the whole array.