-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlidingWindowMaximum.java
More file actions
21 lines (21 loc) · 921 Bytes
/
Copy pathSlidingWindowMaximum.java
File metadata and controls
21 lines (21 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] ans = new int[n - k + 1];
Deque<Integer> dq = new ArrayDeque<>();
int p = 0; //Points to the index in the ans array.
for (int i = 0; i < n; i++) {
//Condition for deleting the element from front of the queue which is out of bound of k range
if (!dq.isEmpty() && dq.peek() == i - k)
dq.removeFirst();
//Condition for removing all the smaller element index from the i th element of the array from the deque
while (!dq.isEmpty() && nums[dq.peekLast()] < nums[i]) {
dq.removeLast();
}
//Inserting the current element in the queue
dq.addLast(i);
if (!dq.isEmpty() && i >= k - 1) {
ans[p++] = nums[dq.peekFirst()];
}
}
return ans;
}