Check whether a hiker can reach the last marker

Write codeEasy10 minAmazon and Meta style

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 marker i.
  • Returns true if you can reach the last marker and false if you cannot.

Rules:

  • You start on marker 0.
  • From marker i you may move forward to any marker from i + 1 to i + 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]) -> bool
Example 1
Inputcan_finish_trail([2, 3, 1, 1, 4])
OutputTrue
Example 2
Inputcan_finish_trail([3, 2, 1, 0, 4])
OutputFalse