Jump Game - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

The algorithm determines whether we can reach the last index in the array using a greedy reachability check. We maintain a single variable, farthestReachable, representing the furthest index that can currently be reached.

  • We iterate through the array from left to right.
    • If the currentIndex is greater than farthestReachable, it means no earlier position can reach currentIndex, creating a gap in reachability. Since every future position depends on a previous reachable one, we must immediately stop and return false.
    • Otherwise, currentIndex lies within the reachable range. We compute how far we can jump from here using newReach = currentIndex + nums[currentIndex], and update: farthestReachable = max(farthestReachable, newReach)
    • If at any point farthestReachable ≥ lastIndex, it means the reachable segment already covers the target, so we can safely return true.

🧠 Key Intuition

  • Continuous reachability: From any index j, you may choose any jump distance between 0 and nums[j]. If a farther index k is reachable from j, then all intermediate indices between j and k are also reachable by taking shorter jumps.
  • Inductive linkage: Every reachable index k > 0 is reached from some earlier reachable index j < k. Combining this with the contiguous-range property above, we conclude that reachability expands outward as a continuous prefix — meaning if k is reachable, all indices in [0, k] are reachable (0 is included since it is the starting point).
  • At iteration currentIndex:
    • If currentIndex ≤ farthestReachable, it lies within the current continuous reachable segment — meaning it can be reached from some earlier index or is the start itself.
    • If currentIndex > farthestReachable, it cannot be reached from any previous reachable index, nor can it be the start, so it is unreachable. Since every future step relies on a reachable predecessor, the process must terminate with false.

📈 Flow Summary

  1. Initialize farthestReachable = 0.
  2. For each index currentIndex:
    • If currentIndex > farthestReachable → return false.
    • Compute newReach = currentIndex + nums[currentIndex] and update farthestReachable as max(farthestReachable, newReach).
    • If farthestReachable ≥ lastIndex → return true.

Time Complexity

The algorithm makes a single pass through the array, updating farthestReachable at each step using constant-time operations. Therefore, the total running time is O(n).

Space Complexity

The algorithm maintains only a few scalar variables — currentIndex, farthestReachable, and lastIndex — without any additional data structures. Thus, the space complexity is O(1).

Visucodize editorial solution titled JS Solution for LeetCode's Jump Game coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/jump-game.