Minimum no. of Jumps to reach end of an array
Pattern: Jump game
Think of layers: indexes reachable in 0 jumps, then 1, and so on. Scan forward tracking the farthest reach; when you reach the end of the current layer, take one more jump and the next layer ends at that farthest reach. If the reach does not grow, the end is unreachable.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Each value is the longest jump you may make from that index. Return the fewest jumps from index 0 to the last index,
or -1 if it cannot be reached. [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9] gives 3; [0, 10, 20] gives -1.
The catalogue has no practice link; LeetCode's Jump Game II is the same task except that it promises the end is reachable.
Think first
- Which indexes can you reach with exactly one jump?
- With at most two jumps?
- When does the next jump become necessary?
Hints
Brute force, and why it fails
BFS over indexes, or DP trying every jump: O(n²).
The key insight
The layers are consecutive windows of indexes, so a BFS needs no queue: one scan with the current layer's end and the farthest reach.
Dry run
| jumps | 1 |
|---|
arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]. Layer 0 is index 0, which reaches 1. Jump 1: layer ends at 1.
The plan
if n == 1: return 0
jumps = end = farthest = 0
for i in 0..n-2:
farthest = max(farthest, i + arr[i])
if i == end:
if farthest <= i: return -1
jumps += 1; end = farthest
if end >= n - 1: return jumps
Complexity
- Time
O(n)- Space
O(1)
One pass.
Edge cases
- One index: 0 jumps.
- A 0 at index 0 with more indexes: -1.
- A zero that a longer jump passes over.
I've built my logic, show the code
Java
class Solution {
public int minJumps(int[] arr) {
int n = arr.length;
if (n == 1) return 0;
int jumps = 0, end = 0, farthest = 0; // [.., end] is reachable with `jumps` jumps
for (int i = 0; i < n - 1; i++) {
farthest = Math.max(farthest, i + arr[i]);
if (i == end) { // the current layer is used up: one more jump
if (farthest <= i) return -1; // nothing beyond this point is reachable
jumps++;
end = farthest;
if (end >= n - 1) return jumps;
}
}
return jumps;
}
}C++
class Solution {
public:
int minJumps(vector<int>& arr) {
int n = static_cast<int>(arr.size());
if (n == 1) return 0;
int jumps = 0, end = 0, farthest = 0; // [.., end] is reachable with `jumps` jumps
for (int i = 0; i < n - 1; i++) {
farthest = max(farthest, i + arr[i]);
if (i == end) { // the current layer is used up: one more jump
if (farthest <= i) return -1; // nothing beyond this point is reachable
jumps++;
end = farthest;
if (end >= n - 1) return jumps;
}
}
return jumps;
}
};Python
class Solution:
def minJumps(self, arr: list[int]) -> int:
n = len(arr)
if n == 1:
return 0
jumps = end = farthest = 0 # [.., end] is reachable with `jumps` jumps
for i in range(n - 1):
farthest = max(farthest, i + arr[i])
if i == end: # the current layer is used up: one more jump
if farthest <= i:
return -1 # nothing beyond this point is reachable
jumps += 1
end = farthest
if end >= n - 1:
return jumps
return jumpsCommon mistakes
- Counting the jump at the last index, giving one too many.
- Missing the -1 case.