Jump Game

Key Idea

Solution

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        # Initialize
        farthest_position = 0
        for i in range(len(nums)):
            # If current index is beyond what we can reach, we are stuck
            if i > farthest_position:
                return False
            # Update the farthest place we can reach
            farthest_position = max(farthest_position, i + nums[i])
            # If we can reach or pass the last index, success
            if farthest_position >= len(nums) - 1:
                return True
        return True

Complexity