forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlidingWindowMaximum.java
53 lines (50 loc) · 1.53 KB
/
SlidingWindowMaximum.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
53
import java.util.*;
/**
* TestCases
[1,-1] 1
[7,2,4] 2
[1,3,1,2,0,5] 3
*/
public class SlidingWindowMaximum {
/**
* 注意PriorityQueue的remove复杂度是O(k),所以本题复杂度是O(n*k)
* 可以将PriorityQueue转成TreeMap,复杂度为O(n*lgk)
*/
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length == 0 || k == 0) {
return new int[0];
}
int[] result = new int[nums.length - k + 1];
Queue<Integer> queue = new PriorityQueue<>(Comparator.<Integer>reverseOrder());
for (int i = 0; i < nums.length; i++) {
queue.offer(nums[i]);
if (queue.size() > k) {
queue.remove(nums[i - k]);
}
if (i - k + 1 >= 0) {
result[i - k + 1] = queue.peek();
}
}
return result;
}
public int[] maxSlidingWindow2(int[] nums, int k) {
if (nums.length == 0 || k == 0) {
return new int[0];
}
int[] result = new int[nums.length - k + 1];
Deque<Integer> queue = new LinkedList<>();
for (int i = 0; i < nums.length; i++) {
while (!queue.isEmpty() && nums[i] > nums[queue.peekLast()]) {
queue.pollLast();
}
queue.offerLast(i);
if (queue.peekFirst() <= i - k) {
queue.pollFirst();
}
if (i - k + 1 >= 0) {
result[i - k + 1] = nums[queue.peekFirst()];
}
}
return result;
}
}