Jump Game [LC#55]

You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise.

Intuition

There is a dp approach. But time complexity is $O(n^2)$ with linear storage. The greedy approach.

Code

def jump_game(nums: List[int]) -> bool:
    n = len(nums)
    left_limit = n - 1
    for pos in range(n - 1, -1, -1):
        if pos + nums[pos] >= left_limit:
            left_limit = pos
    return left_limit == 0

Time complexity