2Brute2Best
πŸ”₯0

Jump Game

MediumΒ·LeetCode #55Basic Anchor
LeetCode
30-Second Mental ModelReachability (farthest reach)
Complexity:O(2^N)β†’O(N)
Key Insight

Keep one number, far: the farthest index we know we can reach. Walk i from left to right. If i is past far, it cannot be reached, so return false. Otherwise i is reachable and extends the block to i + nums[i], so set far = max(far, i + nums[i]).

Core Validity Invariant

Index i is reachable only if i ≀ far, so one comparison between i and far decides whether the walk is stuck.

01

Problem

You start on index 0 of an array nums. Each number is the longest jump you can make from that index: from index i you may land on any index from i + 1 up to i + nums[i], as long as you stay inside the array.

Return true if you can reach the last index, and false otherwise.

Input

An array of integers nums. nums[i] is the furthest forward jump from index i.

Output

true if the last index can be reached from index 0, otherwise false.

Example 1
Input: nums = [2, 3, 1, 1, 4]
Output: true

Jump 1 step from index 0 to index 1 (its value is 3), then jump up to 3 steps from there to the last index.

Example 2
Input: nums = [3, 2, 1, 0, 4]
Output: false

No jump from index 0, 1, 2 or 3 gets past index 3, and index 3 holds 0. So index 4 is never reached.

Constraints
  • β€’ 1 ≀ nums.length ≀ 10^4
  • β€’ 0 ≀ nums[i] ≀ 10^5
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck β€” each hint is counted.

  1. 1.

    Do I need the exact route, or only whether some route reaches the end?

  2. 2.

    If I stand on an index that holds 0, am I always stuck? What if I never have to land on it?

  3. 3.

    Suppose every index up to some point is reachable. Which index is the furthest one I can be sure of?

  4. 4.

    If index i is reachable, which new indexes does it make reachable?

  5. 5.

    Can one number and one pass answer the question, without a table?

03

Brute force approach

04

Better approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceFrom each index, try every jump length one by oneO(2^N)O(N)
BetterFor each index, check whether a reachable earlier index can jump to itO(NΒ²)O(N)
OptimalScan left to right and keep the farthest index you can reach so farO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • β€œjump at most nums[i] steps”From index `i` you can land on any of up to `nums[i]` spots after it. Every jump goes right, never left.
  • β€œreach the last index”We only need a yes or no, so the reachable block is enough. We do not need the routes.
  • β€œstart on index 0”The reachable indexes start at 0 and grow to the right with no holes.
Reachable indexes form one block [0 … far]+far = max(i + nums[i])+stuck when i > farβ†’Reachability (farthest reach)

This is the anchor of the reachability sub-pattern: the whole answer depends on one number, the farthest index reached so far. Jump Game II keeps the same frontier but counts rounds. Jump Game III breaks the rule, because a jump can also go left, so the reachable indexes are no longer one block that starts at the beginning.

10

Don't make this mistake

Pattern confusion: stopping at every zero
βœ— if (nums[i] == 0) return false;
βœ“ if (i > far) return false;

A zero is a dead end only if the walk has to stop on it. On [2, 0, 0] you jump 2 steps from index 0 straight to index 2, the last index, so the answer is true. The wrong check returns false at index 1. What matters is whether index i is inside the block (i ≀ far), not whether its value is 0.

Updating far before checking i
βœ— far = max(far, i + nums[i]); if (i > far) return false;
βœ“ if (i > far) return false; far = max(far, i + nums[i]);

After the update, far is at least i + nums[i], which is at least i. So i > far can never be true, and the check never fires. On [3, 2, 1, 0, 4] the wrong version returns true, but the answer is false: index 4 is past far = 3.

Edge case: a single element
βœ— if (nums[0] == 0) return false;
βœ“ No special case: the loop handles n = 1.

With nums = [0] you already stand on the last index, so the answer is true. The loop starts at index 0, which is also the last index, so it returns true without any jump.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.