Sliding Window Maximum
Problem statement
Given an integer array nums and a window size k, return the maximum value in every consecutive window of exactly k elements. Return the maxima in order from the leftmost window to the rightmost.
For this exercise, assume the array is nonempty and the window size is valid. Values may be negative and may repeat.
Function
maxSlidingWindow(nums: int[], k: int) → int[]Examples
Example 1
nums = [4,2,12,3,8,7]k = 3return = [12,12,12,8]The four windows are [4,2,12], [2,12,3], [12,3,8] and [3,8,7]. Their maxima are 12, 12, 12 and 8.
Example 2
nums = [-4,-2,-9]k = 1return = [-4,-2,-9]Each one-element window returns its own value, including negative values.
Constraints
- For this exercise, assume
1 <= nums.length <= 10^5. 1 <= k <= nums.length.-10^9 <= nums[i] <= 10^9.