Check whether a hiker can reach the last marker
Your task
A hiking game lays out a trail of markers in a row. The number on each marker is how far your stamina lets you move forward from it.
Write can_finish_trail(jumps) that returns whether you can reach the last marker.
Input and output:
jumps: a list of 1 to 100,000 integers, each between 0 and 100,000.jumps[i]is the number on markeri.- Returns true if you can reach the last marker and false if you cannot.
Rules:
- You start on marker 0.
- From marker
iyou may move forward to any marker fromi + 1toi + jumps[i]. You choose where to land. - A move that would go past the end still lets you land on the last marker.
- From a marker with the number 0 you cannot move.
- With a single marker you are already on the last one, so the answer is true.
Examples
can_finish_trail(jumps: list[int]) -> boolExample 1
Input
can_finish_trail([2, 3, 1, 1, 4])Output
TrueExample 2
Input
can_finish_trail([3, 2, 1, 0, 4])Output
False