Jump Game
Decide whether jumps along an array can carry you from its first index to its last.
Given an array numsnums[i]itruefalse
Example 1
Output: true
Explanation: jump 1 step to index 1, then 3 steps to the last index.
Example 2
Output: false
Explanation: every path lands on index 3, whose jump length is 0.
The Idea
Picture a row of stepping stones across a stream. Each stone has a number painted on it: the most stones you may leap forward from there. You stand on the first stone and want to land on the last one. The array numsii
In Example 1, [2, 3, 1, 1, 4][3, 2, 1, 0, 4]
Even five numbers give several paths to follow, and a longer array gives far more. Do we really need to know which path works, or only how far some path can get?
How far is enough. If you can reach an index, you can reach every index before it too. A path to it either lands on each earlier index or jumps over it, and a jump that passes over an index could be made shorter to land there instead. So the indices you can reach always form one unbroken block, from index 0 up to an index we will call the farthest reach.
Look at the picture above. In the top row, labelled "Example 1", the longest jump from index 0 lands on 0 + 2 = 2, and the one from index 1 lands on 1 + 3 = 4, the last index. That row reads "farthest reach becomes 4: true". In the bottom row, "Example 2", the shaded box around index 0 to index 3, labelled "reachable block: farthest reach = 3", is the reachable block. Every jump ends at index 3 or before, so the farthest reach stays 3, and index 4 is marked "cannot be reached".
So one number describes every index we can reach. Walking the array from left to right, each index inside the block may push the farthest reach further, and we never go back to compare paths. Taking the best step we can see at each index and never reconsidering it is what puts this problem among the greedy methods.
Solution Approach
After a look at why following paths is slow, three things need settling: what to remember while walking the array, whether to check an index before using its jump, and when the walk may stop.
Following every path
Why not try the paths one by one? A search can try each jump length from index 0, then each jump length from wherever it lands, and so on. Many paths land on the same index again and again, so the work can grow very fast. Marking each index as reachable, and from each reachable index marking every index its jump can land on, avoids the repeats but still takes up to n × n steps for n numbers.
What to remember
What does the walk need to keep? Only the farthest reach, in a variable named farthestii + nums[i]farthest
Checking before extending
What if the walk arrives at an index outside the block? In Example 2, the walk arrives at index 4 while farthestfalsei > farthest
When the walk stops
If the walk gets through every index without finding one outside the block, the last index was inside it, and the answer is truetruefarthestnums.length - 1
Java Solution
The method canJump
- First, set
to 0: only index 0, the start, is reachable so far.farthest - Walk the array with one loop, moving
from index 0 to the last index.i - Inside the loop, check
first. If it holds, no jump lands on indexi > farthest , so returni .false - Otherwise index
is reachable. Seti tofarthest , the larger of the old reach and the landing spot of the longest jump fromMath.max(farthest, i + nums[i]) .i - If the loop finishes without returning, the last index was inside the block, so return
.true
The main
import java.util.Arrays;
public class JumpGame {
public static boolean canJump(int[] nums) {
int farthest = 0; // farthest index reachable so far
for (int i = 0; i < nums.length; i++) {
if (i > farthest) {
return false; // index i cannot be reached
}
farthest = Math.max(farthest, i + nums[i]);
}
return true;
}
public static void main(String[] args) {
int[][] tests = {
{2, 3, 1, 1, 4},
{3, 2, 1, 0, 4},
{0},
{0, 1},
{2, 0, 0},
{1, 0, 1, 0},
{4, 0, 0, 0, 0, 0}
};
for (int[] nums : tests) {
System.out.println(Arrays.toString(nums) + " -> " + canJump(nums));
}
}
}
Running it prints:
[2, 3, 1, 1, 4] -> true
[3, 2, 1, 0, 4] -> false
[0] -> true
[0, 1] -> false
[2, 0, 0] -> true
[1, 0, 1, 0] -> false
[4, 0, 0, 0, 0, 0] -> false
Look at the second line: [3, 2, 1, 0, 4]false[0] -> true[0, 1]false
In [2, 0, 0][1, 0, 1, 0]falsefarthest
Why i > farthesti >= farthestfarthest>=i = 0farthest[2, 3, 1, 1, 4][0][5, 1, 1]false
Complexity
The method does a fixed amount of work at each index and keeps a single number between them.
- Time: O(n), because the loop looks at each of the n indices at most once, with one comparison and one
, and it stops early at the first index outside the reachable block.Math.max - Space: O(1), because
and the loop index are all the method stores, however long the array is.farthest
Counting the fewest jumps to the last index uses the same farthest reach, one jump at a time: see