Loading…
Loading…
Given an array where each element is the maximum jump length from that position, print true if you can reach the last index starting from index 0.
Input: 2 3 1 1 4
Output: true
Track the farthest index reachable so far. Walk through the array once: if the current index is already beyond the farthest reachable point, you can never get here, so it's impossible. Otherwise, update the farthest reachable point using this position's jump length. No need to try every possible jump combination — the single running "farthest reachable" value is sufficient.
1reach = 0
2for i, n in enumerate(nums):
3 if i > reach:
4 return False
5 reach = max(reach, i + n)
6return TrueLine 1: the array, space-separated
Print true or false.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.