MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / jumpToTheEnd

Function jumpToTheEnd

kotlin/Greedy/JumpToTheEnd.kt:1–16  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1fun jumpToTheEnd(nums: List<Int>): Boolean {
2 // Set the initial destination to the last index in the array.
3 var destination = nums.size - 1
4 // Traverse the array in reverse to see if the destination can be
5 // reached by earlier indexes.
6 for (i in nums.size - 2 downTo 0) {
7 // If we can reach the destination from the current index,
8 // set this index as the new destination.
9 if (i + nums[i] >= destination) {
10 destination = i
11 }
12 }
13 // If the destination is index 0, we can jump to the end from index
14 // 0.
15 return destination == 0
16}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected