Last active
March 8, 2023 19:37
-
-
Save zwhitchcox/7eec4923db0505e3fb3258a56eb98825 to your computer and use it in GitHub Desktop.
findStart
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 findStart(strArr) { | |
const stations = strArr.slice(1).map((station) => station.split(':').map(parseInt)) | |
for (let i = 0; i < stations.length; i++) { | |
for (let i = 0; i < stations.length; i++) { | |
let curGal = 0 | |
let total = 0; | |
for (let j = 0; j < stations.length; j++) { | |
const [gallons] = stations[i + j % stations.length] | |
const [_, needed] = stations[(i + j + 1) % stations.length] | |
curGal += gallons; | |
if (curGal < needed) { | |
break; | |
} else { | |
total++; | |
curGal -= needed; | |
} | |
} | |
if (total === stations.length) { | |
return i + 1; | |
} | |
} | |
} | |
return "impossible"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment