Sliding Window Maximum


Problem Clarification

The sliding window maximum problem involves finding the maximum element in each sliding window of size 'k' in a given array. We need to find the maximum element for each window and return an array containing these maximum values.

Problem Analysis

Input: An array of integers and the window size 'k'.
Output: An array of integers containing the maximum values for each sliding window.
Constraints: The array length is at least equal to the window size, and the window size is greater than zero.
 

Algorithm Design

To solve this problem efficiently, we can use a double-ended queue (deque) to store the indices of elements in the current window. We will iterate through the array, maintaining the following rules:
Remove elements from the front of the deque that are outside the current window.
Remove elements from the back of the deque that are smaller than the current element.
Add the current element index to the deque's back.
At each iteration, the front element of the deque will represent the maximum element for the current window. We will add this maximum to our result array and continue the iteration.

Approach Explanation

Create an empty deque to store indices.

Iterate through the array from index 0 to n-1. a. Remove elements from the front of the deque that are outside the current window (i - k). b. Remove elements from the back of the deque that are smaller than the current element. c. Add the current element index to the deque's back. d. If the current index is greater than or equal to k - 1 (i.e., the first window is complete), add the maximum element (at the front of the deque) to the result array.

Return the result array containing the maximum values for each sliding window.

Code Implementation

def max_sliding_window(nums, k):
    result = []
    deque = []
    
    for i in range(len(nums)):
        # Remove elements from the front of the deque that are outside the current window
        if deque and deque[0] == i - k:
            deque.pop(0)
        
        # Remove elements from the back of the deque that are smaller than the current element
        while deque and nums[i] > nums[deque[-1]]:
            deque.pop()
        
        deque.append(i)
        
        # Add maximum element to the result array
        if i >= k - 1:
            result.append(nums[deque[0]])
    
    return result


Testing

Let's test the code with some example test cases:

# Example 1
nums = [1, 3, -1, -3, 5, 3, 6, 7]
k = 3
print(max_sliding_window(nums, k))
# Output: [3, 3, 5, 5, 6, 7]

# Example 2
nums = [9, 11]
k = 2
print(max_sliding_window(nums, k))
# Output: [11]

# Example 3
nums = [4, -2]
k = 2
print(max_sliding_window(nums, k))
# Output: [4]

# Additional test case
nums = [1, 2, 3, 4, 5, 6, 7]
k = 3
print(max_sliding_window(nums, k))
# Output: [3, 4, 5, 6, 7]

Complexity Analysis

Time Complexity: The algorithm iterates through the array once, so the time complexity is O(n), where n is the length of the array.
Space Complexity: The space complexity is O(k) because the deque stores at most 'k' indices at any time.

Error Handling

The code assumes that the input satisfies the constraints: the array length is at least equal to the window size, and the window size is greater than zero. If these conditions are not met, the behavior of the code may not be as expected.

Discussion

In this solution, we used a deque data structure to efficiently find the maximum element in each sliding window. By removing unnecessary elements and keeping the deque sorted in a decreasing order, we can find the maximum element for each window in O(1) time. The algorithm has a linear time complexity, making it efficient for large input arrays.

 

 

Next Post Previous Post