cheetcode

20. Sliding Window MaximumHard

Return the maximum value in every contiguous window of size k.

You are given an array of integers nums and there is a sliding window of size k moving from the left of the array to the right.

Return the max value in each window position.

Maintain only the current window state as it expands or shrinks. Watch for the exact condition that makes a window valid, invalid, or worth recording.

Examples

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Input: nums = [1], k = 1
Output: [1]

Constraints

  • 1 <= nums.length <= 100,000
  • -10,000 <= nums[i] <= 10,000
  • 1 <= k <= nums.length