This visualization is AI-generated and may contain errors. View original problem on LeetCode

Sliding Window Maximum

Find the maximum value in each window as it slides through the array using a monotonic deque.

Sliding Window Monotonic Deque LC 239 Hard

Problem Statement

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the max sliding window - an array containing the maximum value for each window position.

1000ms
Input Array:
Monotonic Deque (stores indices)
Empty
Result (maximums):
Code
Status
Click Initialize to start

How It Works

Key Insight

A monotonic deque maintains indices in decreasing order of their values. When a new element arrives, we remove all smaller elements from the back (they can never be maximum). The front of the deque always contains the index of the maximum element in the current window.

Algorithm Steps

Complexity Analysis

Metric
Complexity
Explanation
Time
O(n)
Each element is added and removed from deque at most once
Space
O(k)
Deque stores at most k indices