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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.i - x where: i - x >= 0 and 0 < x <= d.In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies.
Constraints:
1 <= arr.length <= 10001 <= arr[i] <= 1051 <= d <= arr.lengthProblem summary: Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[6,4,14,6,8,13,9,7,10,6,12] 2
[3,3,3,3,3] 3
[7,6,5,4,3,2,1] 1
jump-game-vii)jump-game-viii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1340: Jump Game V
class Solution {
private int n;
private int d;
private int[] arr;
private Integer[] f;
public int maxJumps(int[] arr, int d) {
n = arr.length;
this.d = d;
this.arr = arr;
f = new Integer[n];
int ans = 1;
for (int i = 0; i < n; ++i) {
ans = Math.max(ans, dfs(i));
}
return ans;
}
private int dfs(int i) {
if (f[i] != null) {
return f[i];
}
int ans = 1;
for (int j = i - 1; j >= 0; --j) {
if (i - j > d || arr[j] >= arr[i]) {
break;
}
ans = Math.max(ans, 1 + dfs(j));
}
for (int j = i + 1; j < n; ++j) {
if (j - i > d || arr[j] >= arr[i]) {
break;
}
ans = Math.max(ans, 1 + dfs(j));
}
return f[i] = ans;
}
}
// Accepted solution for LeetCode #1340: Jump Game V
func maxJumps(arr []int, d int) (ans int) {
n := len(arr)
f := make([]int, n)
var dfs func(int) int
dfs = func(i int) int {
if f[i] != 0 {
return f[i]
}
ans := 1
for j := i - 1; j >= 0; j-- {
if i-j > d || arr[j] >= arr[i] {
break
}
ans = max(ans, 1+dfs(j))
}
for j := i + 1; j < n; j++ {
if j-i > d || arr[j] >= arr[i] {
break
}
ans = max(ans, 1+dfs(j))
}
f[i] = ans
return ans
}
for i := 0; i < n; i++ {
ans = max(ans, dfs(i))
}
return
}
# Accepted solution for LeetCode #1340: Jump Game V
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
@cache
def dfs(i):
ans = 1
for j in range(i - 1, -1, -1):
if i - j > d or arr[j] >= arr[i]:
break
ans = max(ans, 1 + dfs(j))
for j in range(i + 1, n):
if j - i > d or arr[j] >= arr[i]:
break
ans = max(ans, 1 + dfs(j))
return ans
n = len(arr)
return max(dfs(i) for i in range(n))
// Accepted solution for LeetCode #1340: Jump Game V
struct Solution;
use std::collections::HashMap;
impl Solution {
fn max_jumps(arr: Vec<i32>, d: i32) -> i32 {
let n = arr.len();
let d = d as usize;
let mut memo: HashMap<usize, i32> = HashMap::new();
let mut res = 0;
for i in 0..n {
res = res.max(Self::dp(i, &mut memo, &arr, d, n));
}
res
}
fn dp(start: usize, memo: &mut HashMap<usize, i32>, arr: &[i32], d: usize, n: usize) -> i32 {
if let Some(&res) = memo.get(&start) {
return res;
}
let mut max = 0;
let mut i = 1;
while i <= d && start >= i && arr[start] > arr[start - i] {
max = max.max(Self::dp(start - i, memo, arr, d, n));
i += 1;
}
let mut i = 1;
while i <= d && start + i < n && arr[start] > arr[start + i] {
max = max.max(Self::dp(start + i, memo, arr, d, n));
i += 1;
}
let res = 1 + max;
memo.insert(start, res);
res
}
}
#[test]
fn test() {
let arr = vec![6, 4, 14, 6, 8, 13, 9, 7, 10, 6, 12];
let d = 2;
let res = 4;
assert_eq!(Solution::max_jumps(arr, d), res);
let arr = vec![7, 6, 5, 4, 3, 2, 1];
let d = 1;
let res = 7;
assert_eq!(Solution::max_jumps(arr, d), res);
let arr = vec![7, 1, 7, 1, 7, 1];
let d = 2;
let res = 2;
assert_eq!(Solution::max_jumps(arr, d), res);
let arr = vec![66];
let d = 1;
let res = 1;
assert_eq!(Solution::max_jumps(arr, d), res);
}
// Accepted solution for LeetCode #1340: Jump Game V
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1340: Jump Game V
// class Solution {
// private int n;
// private int d;
// private int[] arr;
// private Integer[] f;
//
// public int maxJumps(int[] arr, int d) {
// n = arr.length;
// this.d = d;
// this.arr = arr;
// f = new Integer[n];
// int ans = 1;
// for (int i = 0; i < n; ++i) {
// ans = Math.max(ans, dfs(i));
// }
// return ans;
// }
//
// private int dfs(int i) {
// if (f[i] != null) {
// return f[i];
// }
// int ans = 1;
// for (int j = i - 1; j >= 0; --j) {
// if (i - j > d || arr[j] >= arr[i]) {
// break;
// }
// ans = Math.max(ans, 1 + dfs(j));
// }
// for (int j = i + 1; j < n; ++j) {
// if (j - i > d || arr[j] >= arr[i]) {
// break;
// }
// ans = Math.max(ans, 1 + dfs(j));
// }
// return f[i] = 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.