numbers = [2, 3, 1, 1, 4]→ trueFrom index 0 reach index 1, then jump from index 1 to the last index.
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.
numbers = [2, 3, 1, 1, 4]→ trueFrom index 0 reach index 1, then jump from index 1 to the last index.
numbers = [3, 2, 1, 0, 4]→ falseEvery reachable path stops at or before index 3; index 4 is beyond the frontier.
numbers = [0]→ trueThe start and destination are the same position.
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.
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.
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.
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.
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.
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.
Index 0, jump 2. Frontier 0 → 2. Reachable.
Position 0 is reachable. Its jump extends the frontier to 2.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.
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…
Loading your saved attempts…
This lesson uses original explanations and examples. For additional practice, see the related LeetCode challenge.