PROBLEM 62 OF 75 · CODE STUDIO

Jump Game

Start at index zero of a non-empty array of non-negative jump lengths. From index i you may move forward up to numbers[i] positions. Return whether the last index is reachable.

Know the exact contract

  • The local studio receives at least one non-negative safe integer. The array is not modified.
  • A jump length is a maximum, not an exact distance. An array of one value is already at its final position.
EXAMPLE 1numbers = [2, 3, 1, 1, 4]→ true

From index 0 reach index 1, then jump from index 1 to the last index.

EXAMPLE 2numbers = [3, 2, 1, 0, 4]→ false

Every reachable path stops at or before index 3; index 4 is beyond the frontier.

EXAMPLE 3numbers = [0]→ true

The start and destination are the same position.

Earn the faster approach

SIMPLE APPROACH

Explore every legal jump as a path and stop when one reaches the last index. This explains the reachability definition, but can revisit the same positions exponentially many times without memoization.

IMPROVED APPROACH

Keep the farthest index reachable from any position already proved reachable. Scan indices from left to right while the current index is within that frontier. Extend it with index + jump length. If it reaches the last index, return true.

Why it works

Before inspecting index i, farthest is the greatest position reachable through a jump from an earlier reachable index. If i exceeds farthest, no earlier position can reach i or any later position. Otherwise its outgoing jumps may safely extend the frontier.

Cost

Each reachable index is inspected at most once and the scan stores one frontier, giving O(n) time and O(1) extra space. No path list is needed.

A tempting mistake

For [3, 2, 1, 0, 4], a large early jump is not enough: the farthest reachable index remains 3, and the final index 4 cannot be reached.

FOLLOW THE ACTUAL STATE

Watch the reachable frontier

Each frame shows the current index, jump length, frontier before inspection, and frontier after the update. The trace stops when success or a gap is proved.

STEP 1 / 2

Index 0, jump 2. Frontier 0 → 2. Reachable.

Position 0 is reachable. Its jump extends the frontier to 2.
Result: true
RECALL BEFORE RECOGNITION

Can you rebuild the reasoning?

Without looking back at the explanation, name the signal, the state you keep, one safe transition, the cost, and a case that breaks a tempting mistake. This writing is private and not automatically graded.

Review the lesson →
INDEPENDENT SOLVE

Now write the code.

Write solve(data). data.numbers is a non-empty array of non-negative jump lengths. Return a boolean indicating whether the last index is reachable. Checks include zero walls, a one-position input, and fresh arrays.

Opening the code studio…

PROVE IT AGAIN TOMORROW

A fresh solve is the test.

Loading your saved attempts…

This lesson uses original explanations and examples. For additional practice, see the related LeetCode challenge.