Skip to content

Instantly share code, notes, and snippets.

@akira-cn
Last active July 7, 2021 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akira-cn/ca03626ece8ac433eec1bff52f53a714 to your computer and use it in GitHub Desktop.
Save akira-cn/ca03626ece8ac433eec1bff52f53a714 to your computer and use it in GitHub Desktop.
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