Sliding Window Maximum

Hard Monotonic deque Sliding window

Problem

Given an integer array and a window length k, return an array of the maximum value in every contiguous window of length k, in order from left to right.

Original: LeetCode 239: Sliding Window Maximum.

Examples

Example 1

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

Example 2

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

Constraints

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

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 239: Sliding Window Maximumleetcode.com
  2. Java SE 17 API: ArrayDequedocs.oracle.com