-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque.java
52 lines (44 loc) · 1.79 KB
/
deque.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*You are given an array of integers 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], 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
*/
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || k == 0) {
return new int[0];
}
int n = nums.length;
int[] res = new int[n - k + 1];
Deque<Integer> deque = new LinkedList<>(); //use of deque aka a double-ended queue instead of declaring a priority queue in every cycle
for (int i = 0; i < n; i++) {
// Remove indices that are out of the current window
if (!deque.isEmpty() && deque.peekFirst() == i - k) {
deque.pollFirst();
}
// Remove elements from the back of the deque that are smaller than the current element
while (!deque.isEmpty() && nums[deque.peekLast()] <= nums[i]) {
deque.pollLast();
}
// Add current element index at the back of the deque
deque.offerLast(i);
// The front of the deque contains the index of the maximum element for the current window
if (i >= k - 1) {
res[i - k + 1] = nums[deque.peekFirst()];
}
}
return res;
}
}