Skip to content

Commit f768bb8

Browse files
committed
Add js solution to leetcode problem:no.0239
1 parent 4b08e83 commit f768bb8

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Diff for: solution/0200-0299/0239.Sliding Window Maximum/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@
8888

8989
```
9090

91+
### **JavaScript**
92+
93+
```js
94+
/**
95+
* @param {number[]} nums
96+
* @param {number} k
97+
* @return {number[]}
98+
*/
99+
var maxSlidingWindow = function (nums, k) {
100+
let len = nums.length;
101+
if (len < k) return [];
102+
let res = [], win = [];
103+
for (let i = 0; i < k; i++) {
104+
while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
105+
win.pop();
106+
win.push(i);
107+
}
108+
res.push(nums[win[0]]);
109+
for (let i = k; i < len; i++) {
110+
while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
111+
win.pop();
112+
if (win.length > 0 && win[0] < i - k + 1)
113+
win.shift();
114+
win.push(i);
115+
res.push(nums[win[0]]);
116+
}
117+
return res;
118+
};
119+
```
120+
91121
### **...**
92122

93123
```

Diff for: solution/0200-0299/0239.Sliding Window Maximum/README_EN.md

+30
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ Window position Max
7979

8080
```
8181

82+
### **JavaScript**
83+
84+
```js
85+
/**
86+
* @param {number[]} nums
87+
* @param {number} k
88+
* @return {number[]}
89+
*/
90+
var maxSlidingWindow = function (nums, k) {
91+
let len = nums.length;
92+
if (len < k) return [];
93+
let res = [], win = [];
94+
for (let i = 0; i < k; i++) {
95+
while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
96+
win.pop();
97+
win.push(i);
98+
}
99+
res.push(nums[win[0]]);
100+
for (let i = k; i < len; i++) {
101+
while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
102+
win.pop();
103+
if (win.length > 0 && win[0] < i - k + 1)
104+
win.shift();
105+
win.push(i);
106+
res.push(nums[win[0]]);
107+
}
108+
return res;
109+
};
110+
```
111+
82112
### **...**
83113

84114
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var maxSlidingWindow = function (nums, k) {
7+
let len = nums.length;
8+
if (len < k) return [];
9+
let res = [], win = [];
10+
for (let i = 0; i < k; i++) {
11+
while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
12+
win.pop();
13+
win.push(i);
14+
}
15+
res.push(nums[win[0]]);
16+
for (let i = k; i < len; i++) {
17+
while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
18+
win.pop();
19+
if (win.length > 0 && win[0] < i - k + 1)
20+
win.shift();
21+
win.push(i);
22+
res.push(nums[win[0]]);
23+
}
24+
return res;
25+
};

0 commit comments

Comments
 (0)