-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution239.java
More file actions
29 lines (25 loc) · 780 Bytes
/
Copy pathSolution239.java
File metadata and controls
29 lines (25 loc) · 780 Bytes
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
package com.leetode;
import java.util.LinkedList;
public class Solution239 {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || k < 1 || nums.length < k) {
return new int[0];
}
LinkedList<Integer> qmax = new LinkedList<>();
int[] res = new int[nums.length - k + 1];
int index = 0;
for (int i = 0; i < nums.length; i++) {
while (!qmax.isEmpty() && nums[qmax.peekLast()] <= nums[i]){
qmax.pollLast();
}
qmax.addLast(i);
if(qmax.peekFirst() == i -k){
qmax.pollFirst();
}
if(i >= k -1){
res[index++]=nums[qmax.pollFirst()];
}
}
return res;
}
}