Tap here to switch tabs
Problem
Submissions
Solution

Solution: Sliding Window Maximum

Statement

Naive approach

A naive approach is to slide the window over the input list and find the maximum in each window separately. We iterate over the input list, calculating the maximum element in each window linearly, and then adding it to the output list. In each subsequent iteration, we update the current window by removing the first element from the current window and adding the incoming element of the input list. Once we are done iterating the input list, we return the output list, containing the maximums of all (nw+1)(n−w+1) windows.

The time complexity of this approach is O(n×w)O(n \times w). Since we only iterate over the input list once to find all the windows, the time complexity of this part will be O(n)O(n). Furthermore, removing the first element, adding the incoming element, and finding the maximum element in the window take O(w)O(w) time. The space complexity of this approach is O(w)O(w), since we maintain a list for the window.

Optimized approach using sliding window

We’ll use a sliding window technique combined with a deque (double-ended queue). The idea is to maintain a deque of indexes of elements in the current window such that their corresponding values are in decreasing order. The front of the deque will always store the index of the maximum element of the window.

As we iterate through nums, for each element:

  • We remove smaller elements from the back of the deque since they can’t be the maximum for the current or any future window containing the current element.

  • If the index at the front of the deque falls out of the current window, we remove it.

  • After processing the first w elements, we start appending the value at the front of the deque to the result list, as it represents the maximum for the current window.

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.

Tap here to switch tabs
Problem
Submissions
Solution

Solution: Sliding Window Maximum

Statement

Naive approach

A naive approach is to slide the window over the input list and find the maximum in each window separately. We iterate over the input list, calculating the maximum element in each window linearly, and then adding it to the output list. In each subsequent iteration, we update the current window by removing the first element from the current window and adding the incoming element of the input list. Once we are done iterating the input list, we return the output list, containing the maximums of all (nw+1)(n−w+1) windows.

The time complexity of this approach is O(n×w)O(n \times w). Since we only iterate over the input list once to find all the windows, the time complexity of this part will be O(n)O(n). Furthermore, removing the first element, adding the incoming element, and finding the maximum element in the window take O(w)O(w) time. The space complexity of this approach is O(w)O(w), since we maintain a list for the window.

Optimized approach using sliding window

We’ll use a sliding window technique combined with a deque (double-ended queue). The idea is to maintain a deque of indexes of elements in the current window such that their corresponding values are in decreasing order. The front of the deque will always store the index of the maximum element of the window.

As we iterate through nums, for each element:

  • We remove smaller elements from the back of the deque since they can’t be the maximum for the current or any future window containing the current element.

  • If the index at the front of the deque falls out of the current window, we remove it.

  • After processing the first w elements, we start appending the value at the front of the deque to the result list, as it represents the maximum for the current window.

Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.