-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode239.java
36 lines (35 loc) · 1.06 KB
/
Leetcode239.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
public class Leetcode239 {
// we have a monotonic queue, the max is always on the first position of the queue.
class MonotonicQueue{
LinkedList<Integer> q = new LinkedList<>();
public void push(int n){
while(!q.isEmpty() && q.getLast() < n)q.pollLast();
q.addLast(n);
}
public int max(){
return q.getFirst();
}
public void pop(int n){
if(n == q.getFirst())q.pollFirst();
}
}
public int[] maxSlidingWindow(int[] nums, int k) {
MonotonicQueue q = new MonotonicQueue();
List<Integer> list = new ArrayList<>();
// we use sliding window here.
for(int i = 0;i <nums.length;i++){
if(i < k-1){
q.push(nums[i]);
}else{
q.push(nums[i]);
list.add(q.max());
q.pop(nums[i-k+1]);
}
}
int[] res = new int[list.size()];
for(int i = 0;i < res.length;i++){
res[i] = list.get(i);
}
return res;
}
}