You are given an array of integers
Return the max sliding window.
Output: [3,3,5,5,6,7]
Explanation:
Window positionMax
----------------------
[1 3 -1] -3 5 3 6 73
1 [3 -1 -3] 5 3 6 73
1 3 [-1 -3 5] 3 6 75
1 3 -1 [-3 5 3] 6 75
1 3 -1 -3 [5 3 6] 76
1 3 -1 -3 5 [3 6 7]7
Output: [1]
Constraints:
1 <= nums.length <= 105 -104 <= nums[i] <= 104 1 <= k <= nums.length
Contents
Solution 1 - Using sliding window technique, and a Deque keep track of maximum numbers with the sliding window of k length
In this approach, we will use a
So, how do we achieve this ?
We will start iterating through the input array
-
If the next element is larger than the
head element, then remove thehead element and add current element as newhead . -
If the the next element is less than
head element, then add it to thetail .
By doing so, we are keeping lergest element at the
Let's understand this with an example,
-
Let's start with the first element in
nums array,1 add it to theDeque 1, , , , , , , . -
Next element in
nums array is3 , compare it with head of deque, it is greater than the head element1 , so remove the head and add current element as head, so the deque becomes3, , , , , , , . -
Next element in
nums array is-1 , compare it with head of deque, it is less than the head element1 , so add it to the tail, so the deque becomes3,-1, , , , , , , . -
Now, we have reached sliding window size of
k , so take the head element and add it to the result3 , also move left pointer element, check if that element is the head, left pointer (index =0) element is1 and this is not same as head, so continue to next element. -
Next element is
-3 , since it is less than the head element3 , so add it to the deque, it becomes3,-1,-3, , , , , , , . -
Now, we have reached sliding window size of
k , so take the head element and add it to the result3 , also move left pointer element, check if that element is the head, left pointer (index =1) element is3 and this is same as head, so remove it from deque and continue to next element, so the deque becomes-1,-3, , , , , , , . -
Next element is
5 , since it is greater than the head element-1 , remove the head, and next element-3 is also less than current element, so remove this element as well. So the deque becomes5, , , , , , , . -
Now, we have reached sliding window size of
k , so take the head element and add it to the result5 , also move left pointer element, check if that element is the head, left pointer (index =2) element is-1 and this is not same as head, so continue to next element. -
Next element is
3 , since it is less than the head element5 , so add it to the tail, so the deque becomes5,3, , , , , , , . -
Now, we have reached sliding window size of
k , so take the head element and add it to the result5 , also move left pointer element, check if that element is the head, left pointer (index =3) element is-3 and this is not same as head, so continue to next element. -
Next element is
6 , since it is greater than the head element5 , remove the head, and next element3 is also less than current element, so remove this element as well. So the deque becomes6, , , , , , , . -
Now, we have reached sliding window size of
k , so take the head element and add it to the result6 , also move left pointer element, check if that element is the head, left pointer (index =4) element is5 and this is not same as head, so continue to next element. -
Next element is
7 , since it is greater than the head element6 , remove the head. So the deque becomes7, , , , , , , . -
Now, we have reached sliding window size of
k , so take the head element and add it to the result7 and we have reached the end of array.
Complexity Analysis:
Time complexity: Above code runs in O(n) time, where
Space complexity: O(n), since we are storing input array elements into a
Above implementations source code can be found at