Last active
July 7, 2021 08:58
-
-
Save akira-cn/ca03626ece8ac433eec1bff52f53a714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function jump(arr) { | |
let cur = 0; | |
const target = arr.length - 1; | |
if(cur >= target) return true; | |
while(cur < target) { | |
const r = arr[cur]; | |
if(r <= 0) return false; | |
if(cur + r >= target) return true; | |
let o = 0, s = cur; | |
for(let i = 1; i <= r; i++) { | |
if(o < cur + i + arr[cur + i]) { | |
o = cur + i + arr[cur + i]; | |
s = cur + i; | |
} | |
} | |
if(cur < s) cur = s; | |
else return false; | |
} | |
return true; | |
} | |
console.log(jump([2, 3, 1, 1, 4])) | |
console.log(jump([3, 2, 1, 0, 4])) | |
console.log(jump([3, 2, 2, 0, 4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment