Last updated on January 14th, 2025 at 06:18 am
Here, we see a Sliding Window Maximum LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Heap, Sliding-Window
Companies
Amazon, Google, Zenefits
Level of Question
Hard

Sliding Window Maximum LeetCode Solution
Table of Contents
1. 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.
Example 1:
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Explanation:
Window position | Max |
[1 3 -1] -3 5 3 6 7 | 3 |
1 [3 -1 -3] 5 3 6 7 | 3 |
1 3 [-1 -3 5] 3 6 7 | 5 |
1 3 -1 [-3 5 3] 6 7 | 5 |
1 3 -1 -3 [5 3 6] 7 | 6 |
1 3 -1 -3 5 [3 6 7] | 7 |
Example 2:
Input: nums = [1], k = 1
Output: [1]
2. Coding Pattern Used in Solution
The coding pattern used in all the provided implementations is Sliding Window with a Monotonic Deque. This pattern is commonly used to solve problems involving subarrays or windows of fixed size, where we need to efficiently compute some property (e.g., maximum, minimum, sum) of the window as it slides over the array.
3. Code Implementation in Different Languages
3.1 Sliding Window Maximum C++
class Solution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> result; if (k == 0) return result; multiset<int> w; for (int i = 0, n = (int)nums.size(); i < n; i++) { if (i >= k) w.erase(w.find(nums[i-k])); w.insert(nums[i]); if (i >= k-1) result.push_back(*w.rbegin()); } return result; } };
3.2 Sliding Window Maximum Java
class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums == null || k <= 0) { return new int[0]; } int n = nums.length; int[] r = new int[n-k+1]; int ri = 0; Deque<Integer> q = new ArrayDeque<>(); for (int i = 0; i < nums.length; i++) { while (!q.isEmpty() && q.peek() < i - k + 1) { q.poll(); } while (!q.isEmpty() && nums[q.peekLast()] < nums[i]) { q.pollLast(); } q.offer(i); if (i >= k - 1) { r[ri++] = nums[q.peek()]; } } return r; } }
3.3 Sliding Window Maximum JavaScript
var maxSlidingWindow = function(nums, k) { const q = []; const res = []; for (let i = 0; i < nums.length; i++) { while (q && nums[q[q.length - 1]] <= nums[i]) { q.pop(); } q.push(i); if (q[0] === i - k) { q.shift(); } if (i >= k - 1) { res.push(nums[q[0]]); } } return res; };
3.4 Sliding Window Maximum Python
class Solution(object): def maxSlidingWindow(self, nums, k): d = collections.deque() out = [] for i, n in enumerate(nums): while d and nums[d[-1]] < n: d.pop() d += i, if d[0] == i - k: d.popleft() if i >= k - 1: out += nums[d[0]], return out
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n * log(k)) | O(k) |
Java | O(n) | O(k) |
JavaScript | O(n) | O(k) |
Python | O(n) | O(k) |
- The Sliding Window with Monotonic Deque pattern is highly efficient for solving problems involving fixed-size windows.
- The C++ implementation, while functional, is less efficient due to the use of a
multiset
. - The Java, JavaScript, and Python implementations are optimal with O(n) time complexity and O(k) space complexity.