SDE & MLE Prep

55. Jump Game

Problem

You start at index 0, and nums[i] is the maximum jump length from index i. Decide whether you can reach the last index.

Example: [2,3,1,1,4] → true; [3,2,1,0,4] → false

Constraints: n ≤ 10⁴.

Track the farthest reachable index; if you ever stand beyond it, you're stuck.

Complexity: O(n) time, O(1) space.

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        reach = 0
        for i, x in enumerate(nums):
            if i > reach:
                return False
            reach = max(reach, i + x)
        return True