Skip to main content

Sliding Window Maximum

HardPremium

Given an array of integers nums and a window size k, slide a window of k numbers from the left end of the array to the right end, one position at a time. Return the largest number in each window.

Example 1

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

The windows are [1, 3, -1], [3, -1, -3], [-1, -3, 5], [-3, 5, 3], [5, 3, 6] and [3, 6, 7].

Example 2

Input: nums = [1], k = 1 Output: [1]

Constraints

  • 1 <= k <= nums.length <= 100000
  • -10000 <= nums[i] <= 10000