Jump Game II
Find the fewest jumps from the first index of an array to the last, by counting rounds of reachable indexes.
You stand at index 0 of an array of integers numsnums[i]nums[i]
Example 1
Output: 2
Explanation: jump from index 0 to index 1, then from index 1 to the last index.
Example 2
Output: 2
The Idea
Picture a row of stepping stones across a stream, numbered 0 to 4 from your bank, just like the indexes of nums
From stone 0, the 2 lets you land on stone 1 or stone 2. Leaping as far as possible sounds right, but stone 2 carries a 1, so you go on to stone 3 and then stone 4: three leaps. The shorter first leap lands on stone 1, whose 3 reaches stone 4 directly: two leaps. So the farthest landing spot is not always the best one. How can you tell where to land without trying every route?
Stop choosing stones and count rounds instead. Before any leap you stand on stone 0; call that round 0. After one leap you can stand on stone 1 or stone 2, which is round 1. Round 2 is every stone you can reach with a second leap from round 1, and so on. Look at the picture below.
The grey box, index 0, is round 0. The blue arrows show its leaps to indexes 1 and 2, the boxes bracketed as round 1, reachable with 1 jump. From index 2, the pale arrow reaches index 3 at most. The orange arrow from index 1 goes further, to 1 + 3 = 4. So round 2, in teal, ends at index 4, the last index, and the answer is two jumps.
Each round is a block of neighbouring indexes with no gaps, because a leap may be shorter than its number allows: whoever can reach index 4 can also reach index 3. So a round is fully described by its last index. The next round ends at the farthest point that any index of the current round can reach, which is the largest value of index plus its number in that round.
Example 2 changes the number at index 2 to 0. Round 1 is still indexes 1 and 2, and index 1 still reaches 1 + 3 = 4, so the 0 does no harm and the answer is again 2.
Exploring in rounds like this, with every index one jump away before any index two jumps away, is called breadth-first search, or BFS. Here no list of indexes is needed, because each round is one block. Settling each round by its farthest reach, without ever going back to change an earlier round, is a greedy choice. Note that it picks the round that reaches farthest, not the longest single leap, which failed from stone 0.
Solution Approach
A simple way looks back from every index, but it is slow. The rounds avoid that, and turning them into code means settling three things: how to describe a round with two numbers, when to count a jump, and where the loop stops.
Looking back from every index
Why not work out the fewest jumps to each index in turn? For each index, look back at every earlier index that can reach it, and take the smallest jump count among them, plus one. That gives the right answer, but each index looks back over all the earlier ones, so an array of n numbers takes about n²/2 checks. The rounds need a single pass, because each round is settled once and never looked at again.
Describing a round with two numbers
What must the code remember about a round? A round has no gaps, so its last index is enough, and the code keeps it in currentEndiii + nums[i]farthest
When to count a jump
When does the current round run out? When icurrentEndfarthestjumpscurrentEndfarthesti = 0
Where the loop stops
Should the loop visit the last index too? Standing on the last index needs no further jump, so the loop stops one index before it. If a round ends exactly on the last index, i
Java Solution
The method jump
- First, set three counters to 0:
, the jumps counted so far;jumps , the last index of the round reached with that many jumps; andcurrentEnd , the farthest index reachable with one jump more.farthest - Walk through the array with one loop, moving
from index 0 up to the index just before the last one.i - At each
, seti tofarthest , so it grows when this index reaches further.Math.max(farthest, i + nums[i]) - When
equalsi , the round is used up: add 1 tocurrentEnd and setjumps tocurrentEnd .farthest - After the loop, return
.jumps
The main
import java.util.Arrays;
public class JumpGameII {
public static int jump(int[] nums) {
int jumps = 0;
int currentEnd = 0; // last index of the round reached with "jumps" jumps
int farthest = 0; // farthest index reachable with one jump more
for (int i = 0; i < nums.length - 1; i++) {
farthest = Math.max(farthest, i + nums[i]);
if (i == currentEnd) { // the whole round is scanned: one more jump
jumps++;
currentEnd = farthest;
}
}
return jumps;
}
public static void main(String[] args) {
int[][] inputs = {
{2, 3, 1, 1, 4},
{2, 3, 0, 1, 4},
{0},
{5, 1, 1},
{1, 1, 1, 1}
};
for (int[] nums : inputs) {
System.out.println(Arrays.toString(nums) + " -> " + jump(nums));
}
}
}
Running it prints:
[2, 3, 1, 1, 4] -> 2
[2, 3, 0, 1, 4] -> 2
[0] -> 0
[5, 1, 1] -> 1
[1, 1, 1, 1] -> 3
For [2, 3, 1, 1, 4]icurrentEndcurrentEnd[2, 3, 0, 1, 4]
[0][5, 1, 1]farthest[1, 1, 1, 1]
The loop bound nums.length - 1i < nums.lengthicurrentEnd[0]
Complexity
The method makes one pass over the array and keeps three numbers.
- Time: O(n), because the loop visits each of the n indexes except the last once, and does one
and one comparison at each.Math.max - Space: O(1), because each round is a block with no gaps, so
andcurrentEnd describe it without any list of indexes to visit.farthest
The same rounds of farthest reach also solve