Created
November 23, 2023 09:46
-
-
Save pablopalacios/4c43bbda77741052ac6b77bd0f45ab76 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 solution(a) { | |
let jumps = 0; | |
let i = 0; | |
let currrentElement = 0 | |
let nextElement = -1 ; | |
let lastElement = -1 ; | |
if(a[i] === 0) return -1 | |
while (a[i]){ | |
lastElement = currrentElement | |
currrentElement = i ; | |
jumps++; | |
i = i + a[i] | |
nextElement = i | |
if (lastElement === nextElement ) | |
return -1 | |
} | |
return jumps | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment