Medium Greedy Choice Greedy
In plain English: Starting at the first position, check if you can hop to the end given each position's maximum jump distance.
Track the farthest index reachable. At each position, update the farthest reach — if you ever land beyond the farthest, you are stuck.
Prompt
Given an array where each element is the max jump length from that position, determine if you can reach the last index starting from index 0.
Try to write it from scratch before scrolling down.
Solution
def can_jump(nums):
farthest = 0
for i in range(len(nums)):
if i > farthest:
return False # stuck: can't reach this position
farthest = max(farthest, i + nums[i])
return True
# Test: can_jump([2,3,1,1,4]) == True
# Test: can_jump([3,2,1,0,4]) == False