Jump Game
Check whether you can jump to the last position
DSA, Medium, Greedy
Which structure or algorithm, and what does it cost.
You stand at position 0 on a line of positions numbered from 0. From position i you may jump forward by any whole number of positions from 1 up to jumps[i]. A position with jumps[i] equal to 0 is a dead end. Return True if you can land on the last position. Otherwise return False.
Examples
Constraints
- 1 <= length of
jumps<= 60,000. - Each
jumps[i]is a whole number between 0 and 30,000.
Your answer must contain
- The simple method and why it is too slow; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(1) extra memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Ask a smaller question. Which positions can you reach at all, whatever path gets you there?
Hint 2 the approach
Scan the positions in order and keep track of which ones you can reach so far, instead of following one path.
Hint 3 the key step
The reachable positions form one unbroken stretch from position 0. Keep its right end, the furthest reach. If you arrive at a position beyond it, you are stuck.
Walkthrough
Full walkthrough brute force to best
Brute force
Mark position 0 as reachable. For each reachable position, mark every position it can jump to, up to jumps[i] positions ahead. Then check the last position. Each position can mark up to n others, so this is O(n^2) time and O(n) memory. With 60,000 positions and jumps up to 30,000 that is about 1.35 billion marks, too slow.
Better method
Work from the right. The last position is good. A position is good when one jump from it can land on a good position, so scan the positions it can reach and stop at the first good one. The answer is whether position 0 is good. The early stop helps in practice, but long runs of bad positions still give O(n^2) time in the worst case, with O(n) memory.
Best method
Keep far, the furthest position you can reach so far. Go through the positions in order. If the position number is larger than far, nothing could reach it, so the answer is False. Otherwise raise far to the larger of its value and the position number plus its jump length. If the loop ends, the answer is True. This is correct because the reachable positions always form an unbroken stretch that starts at position 0, and every position up to far is in it. A shorter jump from a position reaches a part of what its longest jump reaches, so only the furthest edge matters.
Hand trace
Run on [3, 0, 0, 2, 0, 1]. Start with far 0. Position 0: far becomes 0 + 3 = 3. Positions 1 and 2 lie inside the stretch, and their jump length is 0, so far stays 3. Position 3: far becomes 3 + 2 = 5. Position 4 is inside, far stays 5. Position 5 is the last position, and 5 is not more than far, so you can stand on it. The loop ends and the answer is True.
Code
def can_reach_end(jumps):
far = 0 # furthest position reachable so far
for i, h in enumerate(jumps):
if i > far:
return False # nothing reaches position i
far = max(far, i + h)
return TrueTime and memory
O(n) time, because each position is visited once. O(1) memory, because only far is kept.
Common mistakes
- Following one path that always jumps the full length. A shorter jump can land on a position that goes much further. Track the furthest reach, not one path.
- Checking only the last position at the end. A dead end earlier in the row can block the way, so compare each position with
faras you go. - Forgetting the one-position case. You already stand on the last position, so the answer is True even when
jumps[0]is 0.