-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSlidingWindow.java
More file actions
62 lines (55 loc) · 1.72 KB
/
Copy pathMaxSlidingWindow.java
File metadata and controls
62 lines (55 loc) · 1.72 KB
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
54
55
56
57
58
59
60
61
62
package LeetCode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* @FileName: MaxSlidingWindow.java
* @Description: 滑动窗口最大值
* @Author: ABCpril
* @Date: 2021/11/22
*/
public class MaxSlidingWindow {
class MonotonicQueue {
private LinkedList<Integer> q = new LinkedList<>();
public void push(int x) {
// 压扁的过程
while (!q.isEmpty() && q.getLast() < x) {
q.pollLast();
}
q.addLast(x);
}
// 队头为最大值
public int max() {
return q.getFirst();
}
public void pop(int x) {
if (q.getFirst() != x) {
return;
}
q.pollFirst();
}
}
public int[] maxSlidingWindow(int[] nums, int k) {
List<Integer> res = new ArrayList<>();
MonotonicQueue window = new MonotonicQueue();
for (int i = 0; i < nums.length; i++) {
if (i - k + 1 < 0) {
// 窗口滑全就进入else逻辑
window.push(nums[i]);
}
else {
window.push(nums[i]);
res.add(window.max());
// 窗口满了需要为下一次push腾位置
window.pop(nums[i - k + 1]);
// pop的逻辑决定了:如果nums[i-k+1]已经被压扁,单调队列只剩一个元素(窗口实际上
// 呈现123这样的单调递增),此时不会真的把最后一个元素pop出去
}
}
int[] arr = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
arr[i] = res.get(i);
}
return arr;
}
}