Jump Game
LeetCode 55 · View on LeetCode
Greedy: track the farthest index reachable. Scan left to right; if the current index exceeds the farthest reach, we're stuck.
def can_jump(nums: list[int]) -> bool:
farthest = 0
for i, jump in enumerate(nums):
if i > farthest:
return False
farthest = max(farthest, i + jump)
return True
if __name__ == '__main__':
print(can_jump([2, 3, 1, 1, 4])) # True
print(can_jump([3, 2, 1, 0, 4])) # False
print(can_jump([0])) # True