15% off all plans & roadmaps Use code
See plansLeetCode 239 · Solution & editorial
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.
Sliding Window Maximum is a Sliding Window problem, in the Monotonic Queue for Max/Min subpattern. The pattern covers fixed-size, variable-size, and character frequency matching window techniques
Master monotonic queue (deque) to find maximum or minimum in sliding windows efficiently in O(n) time.
The reference solution runs in O(n) time and O(k) space.
Sliding Window Maximum has been reported in interviews at Akuna Capital, Amazon, Apple, Bloomberg, ByteDance, Citadel, Dropbox, Facebook.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Sliding Window Maximum is a Sliding Window problem, in the Monotonic Queue for Max/Min subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.