Jump Game

You start at index 0 of an integer array nums. The value nums[i] is the longest jump you may take from index i: from there you can move forward to any index from i + 1 up to i + nums[i] (a value of 0 means you are stuck on that square).

Return true if some sequence of jumps lands you on the last index, and false otherwise.

Example 1

Input: nums = [2,0,3,0,1,4] Output: true Why: Jump from index 0 to index 2, then take a 3-step jump to index 5, the last one.

Example 2

Input: nums = [3,1,1,0,5] Output: false Why: Every route ends on index 3, whose value is 0, so index 4 is unreachable.

Example 3

Input: nums = [0] Output: true Why: You already stand on the last index, so no jump is needed.

Constraints

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 105

Pattern Path · Intervals & Greedy · problem 114 of 169

You do not need to know which jumps to make, only whether the end is reachable.
If you can reach index j, can you also reach every index before j?
So the reachable indices always form a prefix 0 .. reach. Scan left to right and keep extending reach with i + nums[i].
If the scan arrives at an index beyond reach, you are stuck.
Target: O(n) time, O(1) extra space.

Start simple

Mark reachable indices with DP: for each reachable i, mark i+1 .. i+nums[i]. That is O(n · max jump) time, O(n2) in the worst case.

The key idea

The key observation is that the set of reachable indices has no gaps. If you can land on index j, the jump that got you there passed over every index between its start and j, and it could have stopped on any of them, because jumps may be shorter than the maximum. So everything reachable is a prefix 0 .. reach.

That turns the problem into tracking one number. Walk through the array; at index i:

  • If i > reach, index i cannot be reached, and nothing after it can either: return false.
  • Otherwise extend reach = max(reach, i + nums[i]), and if it already covers the last index, return true.

This is a greedy argument: we never commit to a jump, we just keep the furthest frontier, and since the frontier can only grow, one left-to-right pass decides the answer.

Complexity

  • Time: O(n)
  • Space: O(1)

Python

class Solution:
    def canJump(self, nums):
        reach = 0  # furthest index known to be reachable
        last = len(nums) - 1
        for i, jump in enumerate(nums):
            if i > reach:
                return False  # stuck: nothing reaches index i
            reach = max(reach, i + jump)
            if reach >= last:
                return True
        return True

JavaScript

var canJump = function(nums) {
  let reach = 0; // furthest index known to be reachable
  const last = nums.length - 1;
  for (let i = 0; i < nums.length; i++) {
    if (i > reach) return false; // stuck: nothing reaches index i
    reach = Math.max(reach, i + nums[i]);
    if (reach >= last) return true;
  }
  return true;
};

What the interviewer is checking

The interviewer wants to see you move from a search or DP formulation to the one-variable greedy, and justify it with the "reachable set is a prefix" argument.

Mistakes to avoid

  • Treating nums[i] as the exact jump length instead of the maximum.
  • Not stopping when i passes reach, which lets the scan "jump over" a dead zero.
  • Forgetting that a single-element array is already at the end (true even if the value is 0).
  • Using DFS or BFS over all jumps, which can be quadratic with large values.

Follow-ups to expect

  • Return the minimum number of jumps. (Greedy BFS over reach windows.)
  • What if jumps may go left or right? (BFS on the index graph.)
  • Solve it scanning from the right. (Track the leftmost index that reaches the end.)

In real software

Reachability by an expanding frontier shows up in network hop planning and in checking whether overlapping coverage ranges (sensors, radio towers, cached byte ranges) cover a whole span without a gap.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url