Jump Game II
Count the fewest jumps to reach the end
DSA, Medium, Greedy
Which structure or algorithm, and what does it cost.
Positions on a line are numbered from 0, and you begin at position 0. One jump from position i moves you forward by 1 up to jumps[i] positions, your choice. Return the fewest jumps that land you on the last position. The last position can always be reached. With one position the answer is 0.
Examples
Constraints
- 1 <= length of
jumps<= 50,000. - Each
jumps[i]is a whole number between 0 and 50,000. - The last position can be reached from position 0.
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
Think in rounds. After k jumps, which positions can you stand on? Describe that whole group.
Hint 2 the approach
The group reachable after k jumps is one unbroken block. The next block ends where the best position in the current block can send you.
Hint 3 the key step
Walk the positions and keep the furthest reach seen. At which position are you forced to spend the next jump?
Walkthrough
Full walkthrough brute force to best
Brute force
Let fewest[j] be the fewest jumps to land on position j. For each position j, look back at every earlier position i that can reach j, and take the smallest fewest[i] plus 1. Each position checks up to n earlier ones, so this is O(n^2) time and O(n) memory. With 50,000 positions that is about 1.25 billion checks, too slow.
Best method
Treat the row as rounds. With 0 jumps you stand only on position 0. Walk the positions inside the current block and keep far, the furthest position any of them can reach. When you reach edge, the end of the block, one more jump is needed, so add 1 and set edge to far. This is safe because the best jump from a block is the one that reaches furthest. Any other landing spot in the block reaches no further than far, so the block it opens is part of the one far opens, and it can never save a jump later. Stop before the last position, since you do not jump from it.
Hand trace
Run on [3, 1, 1, 4, 1, 2, 1, 1]. Start with count 0, edge 0 and far 0. Position 0: far becomes 3. It is the edge, so the count becomes 1 and edge becomes 3. Positions 1 and 2: far stays 3. Position 3: far becomes 7. It is the edge, so the count becomes 2 and edge becomes 7. Positions 4, 5 and 6: far stays 7 and none is the edge. Edge 7 is the last position, so the answer is 2.
Code
def min_jumps(jumps):
count = 0
edge = 0 # end of the block reachable with `count` jumps
far = 0 # furthest position seen so far
for i in range(len(jumps) - 1):
far = max(far, i + jumps[i])
if i == edge: # block used up, jump again
count += 1
edge = far
return countTime and memory
O(n) time, one pass. O(1) memory, three numbers.
Common mistakes
- Counting a jump at every position. Count a jump only when you stand on the edge of the current block.
- Choosing the position with the biggest
jumps[i]instead of the biggest reachi + jumps[i]. A big jump length early on can reach less far than a small jump length later. - Looping through the last position. When the edge lands exactly on it, you would count one jump too many.