Paths Subjects Questions Quizzes Pricing Search
Advanced Open Pro

Maximums of Sliding Window

You are given an array of integers nums and an integer k representing the size of a sliding window. The window starts at the left edge of the array and slides one position to the right at a time until it reaches the right edge.

Return an array containing the maximum value in the window at each position it occupies.

Example 1

Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
Output: [3, 3, 5, 5, 6, 7]
Explanation:
  Window [1  3  -1] -1  -3  5  3  6  7  -> max = 3
  Window  1 [3  -1  -3] -3  5  3  6  7  -> max = 3
  Window  1  3 [-1  -3  5] 3  6  7       -> max = 5
  Window  1  3  -1 [-3  5  3] 6  7       -> max = 5
  Window  1  3  -1  -3 [5  3  6] 7       -> max = 6
  Window  1  3  -1  -3  5 [3  6  7]      -> max = 7

Example 2

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

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • -10^4 <= nums[i] <= 10^4

Share this question

← Back to Stacks practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.