Skip to content

Jump Game

Medium Day 5 • Striver Blind 75

Return true if you can reach the last index starting from index 0 with maximum jump lengths given by nums[i].

Example 1:

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

Constraints:

  • 1 <= nums.length <= 10^4

Greedy backward goal post shift.

Greedy Reachability


📊 Step-by-Step Execution (Mermaid Diagram)

Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”
graph TD
Problem["Problem of Size N"] --> Sub["Break into Subproblems DP[i]"]
Sub --> Base["Base Cases: DP[0], DP[1]"]
Base --> Trans["State Transition: DP[i] = f(DP[i-1], DP[i-2], ...)"]
Trans --> Table["Fill DP Table / Variables"]
Table --> Result["Return DP[N]"]

function canJump(nums) {
let goal = nums.length - 1;
for (let i = nums.length - 1; i >= 0; i--) {
if (i + nums[i] >= goal) goal = i;
}
return goal === 0;
}
  • Time Complexity: O(n)
  • Space Complexity: O(1)
  • Explanation: Backward greedy goal post update.

function canJump(nums) {
let goal = nums.length - 1;
for (let i = nums.length - 1; i >= 0; i--) {
if (i + nums[i] >= goal) goal = i;
}
return goal === 0;
}
  • Time Complexity: O(n)
  • Space Complexity: O(1)
  • Explanation: Greedy single pass.

  1. Initialize State: Setup necessary pointers, dynamic programming arrays, or hash maps.
  2. Iterate & Evaluate: Process the input according to the boundary conditions.
  3. Update & Return: Compute the optimal answer and return early or at termination.

Iterate backwards starting from end; if current index plus jump distance reaches goal, shift goal to current index.


  1. Track target goal from right to left.

👉 Solve this problem interactively in the DSA Lab