Pattern Guide
Jump Game Variants
"Reachability and minimum jumps. BFS, greedy, DP on jump range problems."
Jump game problems ask whether a position is reachable or the minimum jumps to reach it. Jump Game I/II: greedy with max-reach tracking. Jump Game III: BFS/DFS for reachability with arbitrary jump directions. Jump Game IV: BFS with grouping by value. Jump Game VI: DP with sliding window deque. The family tests multiple algorithmic approaches on similar problem structures.
Problems you can solve with this pattern
4 problems · click any to start solving
// Jump Game I — can reach end?
function canJump(nums) {
let maxReach = 0;
for (let i = 0; i <= maxReach && i < nums.length; i++)
maxReach = Math.max(maxReach, i + nums[i]);
return maxReach >= nums.length - 1;
}
// Jump Game II — minimum jumps to reach end
function jump(nums) {
let jumps = 0, curEnd = 0, farthest = 0;
for (let i = 0; i < nums.length - 1; i++) {
farthest = Math.max(farthest, i + nums[i]);
if (i === curEnd) { // reached end of current jump range
jumps++;
curEnd = farthest;
}
}
return jumps;
}Jump Game I (can reach end?): track maxReach greedily. If current index > maxReach, stuck. Jump Game II (min jumps): greedy with current reach and next reach. When current reach exhausted, take another jump. Jump Game III (reach 0?): BFS, enqueue index ± jump[index]. Jump Game IV (min jumps, value-grouped): BFS grouping same-value indices, jump to all in same group in one step.
// Jump Game I — can reach end?
function canJump(nums) {
let maxReach = 0;
for (let i = 0; i <= maxReach && i < nums.length; i++)
maxReach = Math.max(maxReach, i + nums[i]);
return maxReach >= nums.length - 1;
}
// Jump Game II — minimum jumps to reach end
function jump(nums) {
let jumps = 0, curEnd = 0, farthest = 0;
for (let i = 0; i < nums.length - 1; i++) {
farthest = Math.max(farthest, i + nums[i]);
if (i === curEnd) { // reached end of current jump range
jumps++;
curEnd = farthest;
}
}
return jumps;
}- Can reach end? → Greedy maxReach (I)
- Min jumps, forward only? → Greedy BFS levels (II)
- Bidirectional jumps, reach 0? → BFS/DFS (III)
- Teleport to same-value indices? → BFS with value groups (IV)
- Max score with window constraint? → DP + monotonic deque (VI)
- Reach index = 0 in string? → Sliding window reachability (VII)
Greedy vs BFS: Greedy works when jumps are monotone (longer = better). BFS needed when jumps go in different directions or teleport between non-adjacent indices.