Python coding

Can you reach the last tile?

You stand on the first tile of a row. jumps[i] is the longest jump forward you may take from tile i; any shorter jump is allowed too. Return True if you can land on the last tile, otherwise False.

jumps has at least one tile and its values are non-negative. Aim for one pass and O(1) extra space.

Besides the tests, one large input (8,000 tiles) with a ~200 ms limit (scaled to your device's speed): brute force won't pass.

  • can_reach_end([2,0,1,0]) → true
  • can_reach_end([1,1,0,1]) → false
  • can_reach_end([2,3,0,0,1]) → true
Ctrl/⌘ + Enter runs · Tab indents
Hint 1

You don't need to choose which jump to take. Ask a simpler question tile by tile: what is the farthest tile reachable so far?

Hint 2

Scan left to right keeping farthest = max(farthest, i + jumps[i]). If you arrive at an index greater than farthest, no route gets there: return False.

Hint 3

The trap is simulating one route: always taking the longest jump can land on a 0 that a shorter jump avoids. Track what is reachable, not a single path.

Solution and why it works
pythonO(n) · O(1)
def can_reach_end(jumps: list[int]) -> bool:
    farthest = 0                         # farthest tile reachable so far
    for i, jump in enumerate(jumps):
        if i > farthest:                 # nothing reaches this tile
            return False
        farthest = max(farthest, i + jump)
    return True

Every tile up to the farthest reachable one is itself reachable, because shorter jumps are allowed. So one number summarizes all routes: scan left to right, extend farthest with i + jumps[i], and fail the moment the scan reaches a tile beyond it. That is O(n) time and O(1) space, where trying routes is exponential. The trap is following a single route: always jumping as far as possible lands on a 0 in [2, 3, 0, 0, 1], yet 0 → 1 → 4 works.