Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed array nums of n integers and an integer target.
You are initially positioned at index 0. In one step, you can jump from index i to any index j such that:
0 <= i < j < n-target <= nums[j] - nums[i] <= targetReturn the maximum number of jumps you can make to reach index n - 1.
If there is no way to reach index n - 1, return -1.
Example 1:
Input: nums = [1,3,6,4,1,2], target = 2 Output: 3 Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 3. - Jump from index 3 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3.
Example 2:
Input: nums = [1,3,6,4,1,2], target = 3 Output: 5 Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 2. - Jump from index 2 to index 3. - Jump from index 3 to index 4. - Jump from index 4 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5.
Example 3:
Input: nums = [1,3,6,4,1,2], target = 0 Output: -1 Explanation: It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.
Constraints:
2 <= nums.length == n <= 1000-109 <= nums[i] <= 1090 <= target <= 2 * 109Problem summary: You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0. In one step, you can jump from index i to any index j such that: 0 <= i < j < n -target <= nums[j] - nums[i] <= target Return the maximum number of jumps you can make to reach index n - 1. If there is no way to reach index n - 1, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,3,6,4,1,2] 2
[1,3,6,4,1,2] 3
[1,3,6,4,1,2] 0
jump-game-ii)frog-jump)jump-game-iii)jump-game-iv)minimum-jumps-to-reach-home)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2770: Maximum Number of Jumps to Reach the Last Index
class Solution {
private Integer[] f;
private int[] nums;
private int n;
private int target;
public int maximumJumps(int[] nums, int target) {
n = nums.length;
this.target = target;
this.nums = nums;
f = new Integer[n];
int ans = dfs(0);
return ans < 0 ? -1 : ans;
}
private int dfs(int i) {
if (i == n - 1) {
return 0;
}
if (f[i] != null) {
return f[i];
}
int ans = -(1 << 30);
for (int j = i + 1; j < n; ++j) {
if (Math.abs(nums[i] - nums[j]) <= target) {
ans = Math.max(ans, 1 + dfs(j));
}
}
return f[i] = ans;
}
}
// Accepted solution for LeetCode #2770: Maximum Number of Jumps to Reach the Last Index
func maximumJumps(nums []int, target int) int {
n := len(nums)
f := make([]int, n)
for i := range f {
f[i] = -1
}
var dfs func(int) int
dfs = func(i int) int {
if i == n-1 {
return 0
}
if f[i] != -1 {
return f[i]
}
f[i] = -(1 << 30)
for j := i + 1; j < n; j++ {
if abs(nums[i]-nums[j]) <= target {
f[i] = max(f[i], 1+dfs(j))
}
}
return f[i]
}
ans := dfs(0)
if ans < 0 {
return -1
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2770: Maximum Number of Jumps to Reach the Last Index
class Solution:
def maximumJumps(self, nums: List[int], target: int) -> int:
@cache
def dfs(i: int) -> int:
if i == n - 1:
return 0
ans = -inf
for j in range(i + 1, n):
if abs(nums[i] - nums[j]) <= target:
ans = max(ans, 1 + dfs(j))
return ans
n = len(nums)
ans = dfs(0)
return -1 if ans < 0 else ans
// Accepted solution for LeetCode #2770: Maximum Number of Jumps to Reach the Last Index
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2770: Maximum Number of Jumps to Reach the Last Index
// class Solution {
// private Integer[] f;
// private int[] nums;
// private int n;
// private int target;
//
// public int maximumJumps(int[] nums, int target) {
// n = nums.length;
// this.target = target;
// this.nums = nums;
// f = new Integer[n];
// int ans = dfs(0);
// return ans < 0 ? -1 : ans;
// }
//
// private int dfs(int i) {
// if (i == n - 1) {
// return 0;
// }
// if (f[i] != null) {
// return f[i];
// }
// int ans = -(1 << 30);
// for (int j = i + 1; j < n; ++j) {
// if (Math.abs(nums[i] - nums[j]) <= target) {
// ans = Math.max(ans, 1 + dfs(j));
// }
// }
// return f[i] = ans;
// }
// }
// Accepted solution for LeetCode #2770: Maximum Number of Jumps to Reach the Last Index
function maximumJumps(nums: number[], target: number): number {
const n = nums.length;
const f: number[] = Array(n).fill(-1);
const dfs = (i: number): number => {
if (i === n - 1) {
return 0;
}
if (f[i] !== -1) {
return f[i];
}
f[i] = -(1 << 30);
for (let j = i + 1; j < n; ++j) {
if (Math.abs(nums[i] - nums[j]) <= target) {
f[i] = Math.max(f[i], 1 + dfs(j));
}
}
return f[i];
};
const ans = dfs(0);
return ans < 0 ? -1 : ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.