#239HardMonotonic Deque

Sliding Window Maximum

Maintain a decreasing deque of indices — O(1) window max at each step.

AmazonGoogleMicrosoftShopee

Problem Statement

You are given an integer array nums and a sliding window of size k which moves from the left of the array to the right. Return the max sliding window — the maximum of each window.

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⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴
  • 1 ≤ k ≤ nums.length

Solutions

1
Brute Force — Scan each window
TimeO(n×k)SpaceO(1)

For each window position, scan k elements to find the max. O(n×k) — too slow for large inputs.

Visual Animation
def maxSlidingWindow(nums: list[int], k: int) -> list[int]:
    return [max(nums[i:i+k]) for i in range(len(nums)-k+1)]

Related Concepts

Deepen your understanding with these related topics from our AI Glossary:

Deepen your understanding

Want to master the core concepts?

Our free AI Glossary covers 190+ topics — from Monotonic Deque to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.

Browse AI Glossary All Problems
39+
AI Models
₹69
Per day used
4
Languages

Stuck? Ask AI to explain it step by step.

Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.

Free to start · No credit card required to explore

Get Started Free
Back to all problems