Largest Value in Every Sliding Window
Problem statement
You are given an integer array nums and a window size k. A window is k values next to each other. It starts over the first k values and slides one place to the right at a time until it covers the last value.
Return the largest value inside each window, in order from the leftmost window to the rightmost. There are nums.length - k + 1 windows.
Function
windowMaximums(nums: int[], k: int) → int[]Examples
Example 1
nums = [2,7,3,1,5,4,6]k = 3return = [7,7,5,5,6]The five windows are [2, 7, 3], [7, 3, 1], [3, 1, 5], [1, 5, 4] and [5, 4, 6]. Their largest values are 7, 7, 5, 5 and 6.
Example 2
nums = [5,4,3,2,1]k = 2return = [5,4,3,2]The values only go down, so each window's largest value is its first one: 5, 4, 3 and 2.
Example 3
nums = [-3,-9,-1]k = 3return = [-1]The window is the whole array, so there is one answer: the largest value, -1.
Constraints
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^41 <= k <= nums.length