Sliding Window Maximum

Given an integer array nums and an integer k, return the maximum value in every contiguous subarray of size k. Example: Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7] Explanation: The maximum is tracked for each sliding window. Pattern focus: Monotonic Deque for Sliding Window Extremes.

Input Format

nums = array of integers, k = window size

Output Format

maximum of each window of size k

Constraints

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

Examples

Example 1:

Input:

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

Output:

[3,3,5,5,6,7]

Explanation:

Keep only useful candidates in a decreasing deque.

Example 2:

Input:

nums = [9,11]
k = 2

Output:

[11]

Explanation:

A single window returns one maximum.

Loading...
Sliding Window Maximum - Monotonic Stack Queue