-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.java
32 lines (30 loc) · 924 Bytes
/
solution.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
public class Solution {
public void addElement(Deque<Integer> dq, int element){
while(!dq.isEmpty() && dq.peekLast() < element){
dq.pollLast();
}
dq.offerLast(element);
}
public void removeElement(Deque<Integer> dq, int element){
if(!dq.isEmpty() && dq.peekFirst() == element){
dq.pollFirst();
}
}
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || k <= 0) {
return new int[0];
}
Deque<Integer> dq = new ArrayDeque<Integer>();
int length = nums.length;
int[] result = new int[length - k +1];
for(int i =0; i <k-1; i++){
addElement(dq, nums[i]);
}
for(int j = k-1; j < nums.length; j++){
addElement(dq, nums[j]);
result[j-k+1] = dq.peekFirst();
removeElement(dq, nums[j-k+1]);
}
return result;
}
}