Keep and Share logo     Log In  |  Mobile View  |  Help  
 
Visiting
 
Select a Color
   
 












01.DS.02.01. Find Maximum in Sliding Window.html -- 1.7 meg
 Find Maximum in Sliding Window - Coderust: Hacking the Coding Interview
Log InJoin for free
Back To Module Home
Data Structures
0% completed
Further Reading
Mark Module as Completed
Ask a Question

Find Maximum in Sliding Window

Given an array of integers, find the maximum value in a window.

Statement#

Given an integer array and a window of size w, find the current maximum value in the window as it slides through the entire array.

Note: If the window size is greater than the array size, we will consider the entire array as a single window.

Example#

Created with Fabric.js 3.6.6-42-536We are given the following array, and we willuse a window of size 3.
1 of 4

Sample input#

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
window_size = 3

Expected output#

[3, 4, 5, 6, 7, 8, 9, 10]

Try it yourself#

C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
using namespace std;
vector<int> FindMaxSlidingWindow(vector<int>& nums, int window_size) {
    // Write your code
    vector<int> result;
    return result;
}
#
#include <iostream> #include <vector> using namespace std; vector<int> FindMaxSlidingWindow(vector<int>& nums, int window_size) { // Write your code vector<int> result; return result; }
Enter to Rename, Shift+Enter to Preview
Test
Save
Reset

Solution#

The algorithm uses the deque data structure to find the maximum in a window. A deque is a double-ended queue where the push and pop operations work in O(1)O(1)O(1) at both ends. It acts as our window. The deque stores elements in decreasing order. The front of the deque contains the index for the maximum value in that particular window.

  • At the start of the algorithm, we search for the maximum value in the first window. The first element’s index is pushed to the front of the deque.
  • If the current element is smaller than the one whose index is at the back of the deque, then its index is pushed in and becomes the new back.
  • If the current element is larger than that of the element at the back of the deque, then the back of the deque is popped repeatedly until we find a higher value. Then we push the index of the current element in as the new back.

We will repeat the following steps each time our window moves to the right:

  • Remove the indices of all elements from the back of the deque, which are smaller than or equal to the current element.
  • If the element no longer falls in the current window, remove the index of the element from the front.
  • Push the current element index at the back of the window.

Let’s visualize an example with a window size equal to 333 on an array.

Created with Fabric.js 3.6.6-42-51-16windowresultThis is the initial state. Our window deque and our results array are bothempty. Our window size 'w' is 3.
1 of 7

Let’s see the code for this algorithm:

C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <deque>
#include <vector>
#include <string>
using namespace std;
vector<int> FindMaxSlidingWindow(vector<int>& nums, int window_size) {
    vector<int> result;
    // Return empty list
    if (nums.size() == 0) {
        return result;
    }
    // If window_size is greater than the array size,
    // set the window_size to nums.size()
    if (window_size > nums.size()) {
        window_size = nums.size();
    }
    // Initializing doubly ended queue for storing indices
    deque<int> window;
    // Find out first maximum in the window_size
    for (int i = 0; i < window_size; ++i) {
        // For every element, remove the previous smaller elements from window
        while (!window.empty() && nums[i] >= nums[window.back()]) {
            window.pop_back();
        }
#
#include <iostream> #include <deque> #include <vector> #include <string> using namespace std; vector<int> FindMaxSlidingWindow(vector<int>& nums, int window_size) { vector<int> result;
Enter to Rename, Shift+Enter to Preview
Run
Save
Reset

Time complexity#

The time complexity of this solution is O(n)O(n)O(n).

Note: Every element is pushed and popped from the deque only once in a single traversal.

Space complexity#

The space complexity of this solution is O(w)O(w)O(w), where www is the window size in this case.

Back
About this Module
Next
Search a Rotated Array
Mark as Completed
Report an Issue


Creation date: Oct 7, 2022 7:32pm     Last modified date: Oct 7, 2022 7:32pm   Last visit date: Dec 7, 2024 11:16am
    Report Objectionable Content