-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path239.滑动窗口最大值.js
60 lines (55 loc) · 1.26 KB
/
239.滑动窗口最大值.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
60
/*
* @lc app=leetcode.cn id=239 lang=javascript
*
* [239] 滑动窗口最大值
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
// tail(pop)->[3,-1,3]->head(push)
class MonotonicQueue {
constructor(nums) {
this._queue = []
}
// add element n to the tail of the queue
push(n) {
// eg: [5,3,2,1] <- 4, imaging the new 4 crashing all the left element
while (this._queue.length && this._queue[this._queue.length - 1] < n) {
this._queue.pop()
}
this._queue.push(n)
}
// if the head of queue is n, then delete it
shift(n) {
if (n == this._queue[0]) {
this._queue.shift()
}
}
// return the max element in the queue
max() {
return this._queue[0]
}
}
var maxSlidingWindow = function(nums, k) {
const window = new MonotonicQueue()
const res = []
for (let i = 0; i < nums.length; i++) {
if (i < k - 1) {
// fill the window with k-1 elements first
window.push(nums[i])
} else {
// window slide left and add new number
window.push(nums[i])
// record the max number
res.push(window.max())
// remove old tail val
window.shift(nums[i - k + 1])
}
}
return res
}
// @lc code=end
module.exports = { maxSlidingWindow }