# Question

Formatted question description: https://leetcode.ca/all/239.html

Hard

## Description

Given an array 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:

Input: nums = [1,3,-1,-3,5,3,6,7], and 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  3  -1  -3 [5  3  6] 7       6
1  3  -1  -3  5 [3  6  7]      7


Note:

You may assume k is always valid, 1 ≤ k ≤ input array’s size for non-empty array.

Could you solve it in linear time?

# Algorithm

Use double-ended queue, where insertion and deletion can take place at both ends of the queue in O(1) time complexity. To make sure a sliding window of size k is maintained, store indices, not actual numbers, in the double-ended queue deque.

For the first k elements from nums, directly add them into deque without considering the window size, since the number of elements does not exceed k. Before adding each element, first check whether the number represented by the last element in deque (this means that if the last element in deque is i, then the number represented by the last eoement in deque is nums[i]) is less than the current number. If so, remove the last element from deque until deque becomes empty or the number represented by the last element in deque is greater than or equal to the current number. The first element in deque is the index of the maximum element in the first window of size k. Obtain the maximum element and put it in the first position of the result array (the first position means index 0).

Then loop over nums from index k to the end. For each index i, first check whether the first element in deque equals i - k. If so, then the first element is not in the current window, so remove the first element. Then if the number represented by the last element in deque is less than the current number, remove the last element from deque until deque becomes empty or the number represented by the last element in deque is greater than or equal to the current number. The first element in deque is the index of the maximum element in the current sliding window of size k. Obtain the maximum element and put it in the current position of the result array.

Finally, return the result array.

Example illustration:

input: 1,3,-1,-3,5,3,6,7

(q: right end in, left front out)

iteration-1: q
iteration-2: q[0,1]
iteration-3: q[0,1,2]

iteration-4: poll: q[1,2]
poll-last: none
offer q[1,2,3]
max: nums

iteration-5: poll: q[2,3]
poll-last: q
poll-last: q[]
offer: q
max: nums

iteration-6: poll: none
poll-last: none
offer: q[4,5]
max: nums

iteration-7: poll: none
poll-last: q
poll-last: q[]
offer: q
max: nums

iteration-8: poll: none
poll-last: q[]
offer: q
max: nums


# Code

Java

• import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class Sliding_Window_Maximum {

public static void main(String[] args) {
Sliding_Window_Maximum out = new Sliding_Window_Maximum();
Solution s = out.new Solution();

System.out.println(Arrays.toString(s.maxSlidingWindow(new int[]{1,3,-1,-3,5,3,6,7}, 3)));
}

/*
Deque<String> dq = new ArrayDeque<>();
dq.offer("a");
dq.offer("b");
dq.offer("c");
System.out.println(dq.peek()); // a
System.out.println(dq.peekFirst()); // a
System.out.println(dq.peekLast()); // c
*/

class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return new int;
}

int n = nums.length;
int[] result = new int[n - k + 1];
int resultPointer = 0;

// store index of nums array
Deque<Integer> q =  new ArrayDeque<>();

for (int i = 0; i < nums.length; i++) {
// remove index not in k-window
while (!q.isEmpty() && q.peek() < i - k + 1) { // peek() head of queue
q.poll();
}

// remove use-less index in q
while (!q.isEmpty() && nums[q.peekLast()] < nums[i]) { // peekLast() last of queue
q.pollLast();
}

q.offer(i);

// start from k-th element, there is a max for window
if (i >= k - 1) {
result[resultPointer] = nums[q.peek()];
resultPointer++;
}
}

return result;
}
}
}

• // OJ: https://leetcode.com/problems/sliding-window-maximum/
// Time: O(N)
// Space: O(N)
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& A, int k) {
vector<int> ans;
deque<int> q;
for (int i = 0; i < A.size(); ++i) {
if (q.size() && q.front() == i - k) q.pop_front();
while (q.size() && A[q.back()] <= A[i]) q.pop_back();
q.push_back(i);
if (i >= k - 1) ans.push_back(A[q.front()]);
}
return ans;
}
};

• class Solution(object):
def maxSlidingWindow(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if k == 0:
return []
ans = [0 for _ in range(len(nums) - k + 1)]
stack = collections.deque([])
for i in range(0, k):
while stack and nums[stack[-1]] < nums[i]:
stack.pop()
stack.append(i)
ans = nums[stack]
idx = 0
for i in range(k, len(nums)):
idx += 1
if stack and stack == i - k:
stack.popleft()
while stack and nums[stack[-1]] < nums[i]:
stack.pop()
stack.append(i)
ans[idx] = nums[stack]

return ans