Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-072

Jump Game

Problem

Given nums where nums[i] is the maximum jump length from index i, return True if you can reach the last index starting from index 0, and False otherwise.

Greedily track the farthest reachable index; if the current index ever exceeds it, the end is unreachable. A single-element list is already at the last index.

Hint

Extend the reachable frontier with i + nums[i] as long as i never overtakes it.

Test cases

case 1
in: [[2,3,1,1,4]]
expected: true
case 2
in: [[3,2,1,0,4]]
expected: false
case 3
in: [[0]]
expected: true
case 4
in: [[0,1]]
expected: false
case 5
in: [[2,0,0]]
expected: true
Starter codePython
def can_jump(nums):
    # Your code here
    pass

5 test cases · run your code against them in the editor