Continue learning
Practice related DSA questions in the same topic.
Each entry is the maximum forward jump from that position. Return 1 if the final position is reachable from index zero, otherwise 0.
Each entry is the maximum forward jump from that position. Return 1 if the final position is reachable from index zero, otherwise 0.
def solve(values, target): reach = 0 i = 0 while (i < len(values)): if (i > reach): return 0 reach = max(reach, (i + values[i])) i = (i + 1) return 1Verify that position 0 lies within the frontier 0.
Verify that position 0 lies within the frontier 0.
A row of relay stations extends a signal only when the signal reaches the station.
Keep the furthest position reachable by processing only positions already reachable.
O(n) time · O(1) auxiliary space
Worked example · {"values":[2,3,1,1,4],"target":0} ⇒ 1
Edge case 1 · {"values":[1,0,2],"target":0} ⇒ 0
Edge case 2 · {"values":[0],"target":0} ⇒ 1
Using jump lengths from unreachable positions can create a fictitious route.
Why is every index up to the frontier reachable?