Editor

Jump to the End

#69 · Greedy · Intermediate · 65 XP · about 10 min

You stand on the first square of a row of squares. jumps[i] is the longest jump you can make from square i: you may move forward any number of squares from 1 up to jumps[i], and a 0 means you cannot move from there.

Write can_reach_end(jumps) that returns True if you can reach the last square, and False otherwise.

Examples

can_reach_end([1, 2, 0, 1, 0])  # returns True
can_reach_end([3, 1, 1, 0, 2])  # returns False
can_reach_end([0])              # returns True

In the second example every route ends up on the square holding 0, and nothing can jump past it.

Constraints

  • 1 <= len(jumps) <= 10000
  • 0 <= jumps[i] <= 10000

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.