Advertisement

Sliding Window Maximum - LeetCode 239 Solution

Sliding Window Maximum - Complete Solution Guide

Sliding Window Maximum is LeetCode problem 239, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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

Detailed Explanation

The problem asks us to find the maximum value within a sliding window of size 'k' as it moves from left to right across an array of integers 'nums'. The window encompasses 'k' consecutive elements, and we must determine the maximum element within that window for each position as it slides. The output should be an array containing these maximum values.

Solution Approach

The solution uses a monotonic decreasing deque to keep track of the indices of potential maximum elements within the current sliding window. The deque stores indices, not the values themselves. For each element in the input array, we first remove any indices from the front of the deque that are outside the current window. Then, we remove any indices from the back of the deque whose corresponding values are smaller than the current element, as they can no longer be the maximum. Finally, we add the index of the current element to the back of the deque. The maximum element for the current window is always the element at the front of the deque.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty deque 'q' and an empty output array 'output'.
  2. Step 2: Iterate through the input array 'nums' using an index 'i'.
  3. Step 3: Check if the deque is not empty and if the element at the front of the deque (q[0]) is outside the current window (i.e., q[0] == i - k). If so, remove it from the front.
  4. Step 4: While the deque is not empty and the value at the back of the deque (nums[q[-1]]) is smaller than the current value (nums[i]), remove elements from the back of the deque. This maintains the monotonic decreasing property.
  5. Step 5: Append the current index 'i' to the back of the deque.
  6. Step 6: If the current index 'i' is greater than or equal to k - 1 (meaning the window is full), add the value at the front of the deque (nums[q[0]]) to the 'output' array, as it represents the maximum value in the current window.
  7. Step 7: Return the 'output' array.

Key Insights

  • Insight 1: We need an efficient way to track the potential maximums within the current window. A naive approach of iterating through each window to find the maximum would be inefficient.
  • Insight 2: A monotonic decreasing queue (deque) is the ideal data structure. It allows us to maintain a queue where the elements are sorted in decreasing order, ensuring the maximum element is always at the front. The queue stores indices rather than values, enabling easy out-of-window checks.
  • Insight 3: When a new element is encountered, we need to remove smaller elements from the back of the queue to maintain the monotonic property. We also need to remove elements from the front of the queue if they are no longer within the current window.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(k)

Topics

This problem involves: Array, Queue, Sliding Window, Heap (Priority Queue), Monotonic Queue.

Companies

Asked at: Autodesk, Booking.com, Cisco, Citadel, Coupang, DE Shaw, DoorDash, Flipkart, Gameskraft, Gojek, Goldman Sachs, Juspay, LINE, MakeMyTrip, Media.net, MongoDB, Nuro, Nutanix, Oracle, Palo Alto Networks, PhonePe, Roku, Rubrik, ServiceNow, Visa, Wayfair, Zenefits, Zepto, oyo, tcs.