InferenceAI

Enter at least two characters to search.

Start free

Window Maximums

hardSliding Window40 min50 XP

Return a list with the maximum of each contiguous window of size k in nums, from left to right.

Requirements

  • Run in O(n)O(n) time using a monotonic deque.

Constraints

  • 1≤k≤n1 \le k \le n

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[1,3,-1,-3,5,3,6,7],3]
TASKwindow_maximums()
EXPECTED OUTPUT[3,3,5,5,6,7]

Start with the input values shown in this example.

Example 1 of 3

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

Output: [3,3,5,5,6,7]

Explanation: classic

Input: nums = [1], k = 1

Output: [1]

Explanation: single

Input: nums = [9,8,7,6], k = 2

Output: [9,8,7]

Explanation: decreasing

Hint 1

Keep indices in a deque whose values are decreasing.

Hint 2

Drop indices that fall out of the window from the front.

Topics

DSASliding WindowPython