Created
December 19, 2022 22:04
-
-
Save mchernyavskaya/58396e468922e7c72dc141a05b745e9e 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
fun travelToRome(cities: Array<Int>): Int { | |
val destinationIndex = cities.size - 1 | |
var index = 0 | |
var days = 1 | |
while ((index + cities[index]) < destinationIndex) { | |
val windowStart = 1 | |
val windowEnd = cities[index] | |
var maxIndex = windowStart | |
// where I can jump from window start | |
var maxPos = cities[windowStart] + windowStart | |
for (i in windowStart..windowEnd) { | |
if (cities[i] + i > maxPos) { | |
maxPos = cities[i] + i | |
maxIndex = i | |
} | |
} | |
index = maxIndex | |
days++ | |
} | |
return days | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment