Jump Game
franklinqin0 ArrayGreedy
# Solution
Let be the length of the array nums.
# Greedy Algorithm
Complexity
time:
space:
def canJump(self, nums: List[int]) -> bool:
n = len(nums)
max_pos = 0
for i, jump in enumerate(nums):
if i > max_pos: # current index is beyond reach
return False
if i + jump > max_pos: # update furthest reached point
max_pos = i + jump
if max_pos >= n-1: # can reach last position
return True
return False
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11