-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliding-window-maximum.js
59 lines (52 loc) · 1.08 KB
/
sliding-window-maximum.js
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
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var maxSlidingWindow = function (nums, k) {
// 单调队列
const deque = new Deque();
const result = [];
// 先将前 k 个元素放进队列
for (let i = 0; i < k; i++) {
deque.push(nums[i]);
}
// 记录前 k 个元素的最大值
result.push(deque.front());
for (let i = k; i < nums.length; i++) {
const curr = nums[i];
// 滑动窗口移除最前面元素
deque.pop(nums[i - k]);
// 滑动窗口前加入最后面的元素
deque.push(curr);
// 记录对应的最大值
result.push(deque.front());
}
return result;
};
/**
* 单调队列 (递减)
*/
class Deque {
constructor() {
this.queue = [];
}
push(val) {
if (this.queue.length === 0) {
this.queue.push(val);
} else {
while (this.queue.length > 0 && val > this.queue.at(-1)) {
this.queue.pop();
}
this.queue.push(val);
}
}
pop(val) {
if (val === this.front()) {
this.queue.shift();
}
}
front() {
return this.queue[0];
}
}