Skip to content

Commit 1aaa25e

Browse files
authored
feat: add javascript solution to lc problem: No.1871.Jump Game VII (#404)
1 parent 8a4adfd commit 1aaa25e

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

solution/1800-1899/1871.Jump Game VII/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,35 @@ class Solution {
102102
}
103103
```
104104

105+
### **JavaScript**
106+
107+
```js
108+
/**
109+
* @param {string} s
110+
* @param {number} minJump
111+
* @param {number} maxJump
112+
* @return {boolean}
113+
*/
114+
var canReach = function(s, minJump, maxJump) {
115+
let n = s.length;
116+
let dp = new Array(n).fill(0);
117+
let sum = new Array(n + 1).fill(0);
118+
dp[0] = 1;
119+
sum[1] = 1;
120+
for (let i = 1; i < n; i++) {
121+
if (s.charAt(i) == '0') {
122+
let left = Math.max(0, i - maxJump);
123+
let right = i - minJump;
124+
if (left <= right && sum[right + 1] - sum[left] > 0) {
125+
dp[i] = 1;
126+
}
127+
}
128+
sum[i + 1] = sum[i] + dp[i];
129+
}
130+
return dp.pop();
131+
};
132+
```
133+
105134
### **...**
106135

107136
```

solution/1800-1899/1871.Jump Game VII/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ class Solution {
9090
}
9191
```
9292

93+
### **JavaScript**
94+
95+
```js
96+
/**
97+
* @param {string} s
98+
* @param {number} minJump
99+
* @param {number} maxJump
100+
* @return {boolean}
101+
*/
102+
var canReach = function(s, minJump, maxJump) {
103+
let n = s.length;
104+
let dp = new Array(n).fill(0);
105+
let sum = new Array(n + 1).fill(0);
106+
dp[0] = 1;
107+
sum[1] = 1;
108+
for (let i = 1; i < n; i++) {
109+
if (s.charAt(i) == '0') {
110+
let left = Math.max(0, i - maxJump);
111+
let right = i - minJump;
112+
if (left <= right && sum[right + 1] - sum[left] > 0) {
113+
dp[i] = 1;
114+
}
115+
}
116+
sum[i + 1] = sum[i] + dp[i];
117+
}
118+
return dp.pop();
119+
};
120+
```
121+
93122
### **...**
94123

95124
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} minJump
4+
* @param {number} maxJump
5+
* @return {boolean}
6+
*/
7+
var canReach = function(s, minJump, maxJump) {
8+
let n = s.length;
9+
let dp = new Array(n).fill(0);
10+
let sum = new Array(n + 1).fill(0);
11+
dp[0] = 1;
12+
sum[1] = 1;
13+
for (let i = 1; i < n; i++) {
14+
if (s.charAt(i) == '0') {
15+
let left = Math.max(0, i - maxJump);
16+
let right = i - minJump;
17+
if (left <= right && sum[right + 1] - sum[left] > 0) {
18+
dp[i] = 1;
19+
}
20+
}
21+
sum[i + 1] = sum[i] + dp[i];
22+
}
23+
return dp.pop();
24+
};

0 commit comments

Comments
 (0)