We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a2d1651 commit 5119e34Copy full SHA for 5119e34
239-sliding-window-maximum.js
@@ -4,24 +4,22 @@
4
* @return {number[]}
5
*/
6
const maxSlidingWindow = function(nums, k) {
7
- let n = nums.length
8
- if (n === 0) {
9
- return nums
10
- }
11
- let result = []
12
-
13
- let dq = []
14
- for (let i = 0; i < n; i++) {
15
- if (dq.length && dq[0] < i - k + 1) {
16
- dq.shift()
+ const n = nums.length
+ const stk = []
+ const res = []
+
+ for(let i = 0; i < n; i++) {
+ while(stk.length && stk[0] < i - k + 1) {
+ stk.shift()
17
}
18
- while (dq.length && nums[i] >= nums[dq[dq.length - 1]]) {
19
- dq.pop()
+ while(stk.length && nums[stk[stk.length - 1]] <= nums[i]) {
+ stk.pop()
20
21
- dq.push(i)
22
- if (i - k + 1 >= 0) {
23
- result[i - k + 1] = nums[dq[0]]
+ stk.push(i)
+ if(i >= k - 1) {
+ res.push(nums[stk[0]])
24
25
26
- return result
27
-}
+ return res
+};
0 commit comments