Quick Overview

Given an integer array and a window length k, return the maximum value of every contiguous window of length k as the window slides from left to right. The problem tests efficient maintenance of a moving window on arrays of up to 100,000 elements, where rescanning each window is too slow.

Maximum of Each Length-k Window in an Integer Array

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given an integer array `nums` and a window length `k`, slide a window of exactly `k` consecutive elements from the left end of the array to the right end, one position at a time. Return the maximum value inside the window at each position, in order from left to right. ### Function Signature ```python def window_maxima(nums: list[int], k: int) -> list[int]: ``` ### Rules - The window that starts at index `i` covers `nums[i]` through `nums[i + k - 1]`. - The output has exactly `len(nums) - k + 1` values; the value at index `i` is the maximum of the window that starts at index `i`. - Equal values are fine: the output holds values, not positions. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= k <= len(nums)` - `-10^9 <= nums[i] <= 10^9`, so every value fits in a 32-bit signed integer. ### Examples **Example 1** ```text Input: nums = [4, 2, 12, 3, 8, 1, 7], k = 3 Output: [12, 12, 12, 8, 8] ``` The windows are `[4, 2, 12]`, `[2, 12, 3]`, `[12, 3, 8]`, `[3, 8, 1]` and `[8, 1, 7]`. **Example 2** ```text Input: nums = [5, -1, -3, 2], k = 1 Output: [5, -1, -3, 2] ``` Each window holds one element, so the output equals the input. **Example 3** ```text Input: nums = [-2, -5, -1], k = 3 Output: [-1] ``` There is a single window, the whole array.

Overview: Given an integer array and a window length k, return the maximum value of every contiguous window of length k as the window slides from left to right. The problem tests efficient maintenance of a moving window on arrays of up to 100,000 elements, where rescanning each window is too slow.

Given an integer array `nums` and a window length `k`, slide a window of exactly `k` consecutive elements from the left end of the array to the right end, one position at a time. Return the maximum value inside the window at each position, in order from left to right. Implement `window_maxima(nums, k)`, which returns a list of integers. ### Rules - The window that starts at index `i` covers `nums[i]` through `nums[i + k - 1]`. - The output has exactly `len(nums) - k + 1` values; the value at index `i` is the maximum of the window that starts at index `i`. - Equal values are fine: the output holds values, not positions. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= k <= len(nums)` - `-10^9 <= nums[i] <= 10^9`, so every value fits in a 32-bit signed integer. Every output value is an element of `nums`, so no input or output value can exceed 2^31 - 1 (Java and C++ use `int`). ### Example 1 ```text Input: nums = [4, 2, 12, 3, 8, 1, 7], k = 3 Output: [12, 12, 12, 8, 8] ``` The windows are `[4, 2, 12]`, `[2, 12, 3]`, `[12, 3, 8]`, `[3, 8, 1]` and `[8, 1, 7]`. ### Example 2 ```text Input: nums = [-2, -5, -1], k = 3 Output: [-1] ``` There is a single window, the whole array.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= k <= len(nums)
  • -10^9 <= nums[i] <= 10^9, so every value fits in a 32-bit signed integer

Examples

Input: ([4, 2, 12, 3, 8, 1, 7], 3)

Expected Output: [12, 12, 12, 8, 8]

Explanation: Source example 1: the maximum 12 covers three windows, then 8 takes over; 7 - 3 + 1 = 5 values.

Input: ([-2, -5, -1], 3)

Expected Output: [-1]

Explanation: Source example 3: k = len(nums), one window covering the whole all-negative array.

Hints

  1. Recomputing every window from scratch costs up to len(nums) * k comparisons; with len(nums) up to 10^5 and k up to len(nums), look for work that neighbouring windows can share.
  2. Moving the window one position removes exactly one element on the left and adds exactly one on the right.
  3. The output holds values, not positions, so equal values need no tie-breaking.

Loading coding console...

Show the approach

Approach

Keep a double-ended queue of indices, stored in an array with head and tail pointers. Invariant: after processing index i, the queue holds indices from the current window [i - k + 1, i] in increasing index order, and their values strictly decrease from front to back, so the front index holds the window maximum. For each i: (1) drop the front index while it is <= i - k, because it has left the window; (2) pop indices from the back while their value is <= nums[i], because such an element can never be the maximum of a window that also contains i, and an equal later value reports the same maximum; (3) push i; (4) once i >= k - 1, append nums[front]. Correctness: an index popped from the back is dominated by i in every window that still contains it, so the true maximum of each window is never discarded, and the expiry step guarantees the front lies inside the window; with strictly decreasing values the front is therefore the window maximum. Each index is pushed once and removed at most once, so the total work is linear. Edge cases: k = 1 returns nums unchanged; k = len(nums) returns one value, the whole-array maximum; all-negative input works because no sentinel such as 0 is used; repeated maxima are handled because the output holds values, not positions; the output always has exactly len(nums) - k + 1 values.

Time complexity:
O(n)
Space complexity:
O(n)