Jump Game
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.
Input and constraints
- Supply at least one value.
- Each value must be nonnegative.
- Use at most 32 integers. Each numeric input is between -1000 and 1000.
Code inspector
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 1Visualizer
Step 1 of 6Inspect the current state
Verify that position 0 lies within the frontier 0.
Variable watch
Step explanation
Verify that position 0 lies within the frontier 0.
Worked explanation
A row of relay stations extends a signal only when the signal reaches the station.
Guided solution
Keep the furthest position reachable by processing only positions already reachable.
O(n) time · O(1) auxiliary space
Examples
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
Progressive hints
Hint 1
A row of relay stations extends a signal only when the signal reaches the station.Hint 2
Keep the furthest position reachable by processing only positions already reachable.Hint 3
Step through the worked example and inspect the highlighted variables before each update.Common mistake
Using jump lengths from unreachable positions can create a fictitious route.
Interview follow-up
Why is every index up to the frontier reachable?