239. Sliding Window Maximum LeetCode Solution

In this guide, you will get 239. Sliding Window Maximum LeetCode Solution with the best time and space complexity. The solution to Sliding Window Maximum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sliding Window Maximum solution in C++
  4. Sliding Window Maximum solution in Java
  5. Sliding Window Maximum solution in Python
  6. Additional Resources
239. Sliding Window Maximum LeetCode Solution image

Problem Statement of Sliding Window Maximum

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]

Constraints:

1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

239. Sliding Window Maximum LeetCode Solution in C++

class Solution {
 public:
  vector<int> maxSlidingWindow(vector<int>& nums, int k) {
    vector<int> ans;
    deque<int> maxQ;

    for (int i = 0; i < nums.size(); ++i) {
      while (!maxQ.empty() && maxQ.back() < nums[i])
        maxQ.pop_back();
      maxQ.push_back(nums[i]);
      if (i >= k && nums[i - k] == maxQ.front())  // out-of-bounds
        maxQ.pop_front();
      if (i >= k - 1)
        ans.push_back(maxQ.front());
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

239. Sliding Window Maximum LeetCode Solution in Java

class Solution {
  public int[] maxSlidingWindow(int[] nums, int k) {
    int[] ans = new int[nums.length - k + 1];
    Deque<Integer> maxQ = new ArrayDeque<>();

    for (int i = 0; i < nums.length; ++i) {
      while (!maxQ.isEmpty() && maxQ.peekLast() < nums[i])
        maxQ.pollLast();
      maxQ.offerLast(nums[i]);
      if (i >= k && nums[i - k] == maxQ.peekFirst()) // out-of-bounds
        maxQ.pollFirst();
      if (i >= k - 1)
        ans[i - k + 1] = maxQ.peekFirst();
    }

    return ans;
  }
}
// code provided by PROGIEZ

239. Sliding Window Maximum LeetCode Solution in Python

class Solution:
  def maxSlidingWindow(self, nums: list[int], k: int) -> list[int]:
    ans = []
    maxQ = collections.deque()

    for i, num in enumerate(nums):
      while maxQ and maxQ[-1] < num:
        maxQ.pop()
      maxQ.append(num)
      if i >= k and nums[i - k] == maxQ[0]:  # out-of-bounds
        maxQ.popleft()
      if i >= k - 1:
        ans.append(maxQ[0])

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.