Skip to content

Instantly share code, notes, and snippets.

@mchernyavskaya
Created December 19, 2022 22:04
Show Gist options
  • Save mchernyavskaya/58396e468922e7c72dc141a05b745e9e to your computer and use it in GitHub Desktop.
Save mchernyavskaya/58396e468922e7c72dc141a05b745e9e to your computer and use it in GitHub Desktop.
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