Skip to content

Commit 03867ff

Browse files
authored
Update 1871-jump-game-vii.js
1 parent a197b4f commit 03867ff

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1871-jump-game-vii.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,26 @@ const canReach = function(s, minJump, maxJump) {
7474
}
7575
return dp[n - 1]
7676
};
77+
78+
// another
79+
80+
/**
81+
* @param {string} s
82+
* @param {number} minJump
83+
* @param {number} maxJump
84+
* @return {boolean}
85+
*/
86+
const canReach = function(s, minJump, maxJump) {
87+
const n = s.length
88+
const dp = Array(n).fill(0)
89+
dp[0] = 1
90+
let pre = 0
91+
for(let i = 1; i < n; i++) {
92+
if(i < minJump) continue
93+
if(i >= minJump) pre += dp[i - minJump]
94+
if(i > maxJump) pre -= dp[i - maxJump - 1]
95+
dp[i] = pre > 0 && s[i] === '0' ? 1 : 0
96+
}
97+
98+
return dp[n - 1] ? true : false
99+
};

0 commit comments

Comments
 (0)