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 an integer array nums.
From any index i, you can jump to another index j under the following rules:
j where j > i is allowed only if nums[j] < nums[i].j where j < i is allowed only if nums[j] > nums[i].For each index i, find the maximum value in nums that can be reached by following any sequence of valid jumps starting at i.
Return an array ans where ans[i] is the maximum value reachable starting from index i.
Example 1:
Input: nums = [2,1,3]
Output: [2,2,3]
Explanation:
i = 0: No jump increases the value.i = 1: Jump to j = 0 as nums[j] = 2 is greater than nums[i].i = 2: Since nums[2] = 3 is the maximum value in nums, no jump increases the value.Thus, ans = [2, 2, 3].
Example 2:
Input: nums = [2,3,1]
Output: [3,3,3]
Explanation:
i = 0: Jump forward to j = 2 as nums[j] = 1 is less than nums[i] = 2, then from i = 2 jump to j = 1 as nums[j] = 3 is greater than nums[2].i = 1: Since nums[1] = 3 is the maximum value in nums, no jump increases the value.i = 2: Jump to j = 1 as nums[j] = 3 is greater than nums[2] = 1.Thus, ans = [3, 3, 3].
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: You are given an integer array nums. From any index i, you can jump to another index j under the following rules: Jump to index j where j > i is allowed only if nums[j] < nums[i]. Jump to index j where j < i is allowed only if nums[j] > nums[i]. For each index i, find the maximum value in nums that can be reached by following any sequence of valid jumps starting at i. Return an array ans where ans[i] is the maximum value reachable starting from index i.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[2,1,3]
[2,3,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3660: Jump Game IX
class Solution {
public int[] maxValue(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
int[] preMax = new int[n];
preMax[0] = nums[0];
for (int i = 1; i < n; ++i) {
preMax[i] = Math.max(preMax[i - 1], nums[i]);
}
int sufMin = 1 << 30;
for (int i = n - 1; i >= 0; --i) {
ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i];
sufMin = Math.min(sufMin, nums[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #3660: Jump Game IX
func maxValue(nums []int) []int {
n := len(nums)
ans := make([]int, n)
preMax := make([]int, n)
preMax[0] = nums[0]
for i := 1; i < n; i++ {
preMax[i] = max(preMax[i-1], nums[i])
}
sufMin := 1 << 30
for i := n - 1; i >= 0; i-- {
if preMax[i] > sufMin {
ans[i] = ans[i+1]
} else {
ans[i] = preMax[i]
}
sufMin = min(sufMin, nums[i])
}
return ans
}
# Accepted solution for LeetCode #3660: Jump Game IX
class Solution:
def maxValue(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * n
pre_max = [nums[0]] * n
for i in range(1, n):
pre_max[i] = max(pre_max[i - 1], nums[i])
suf_min = inf
for i in range(n - 1, -1, -1):
ans[i] = ans[i + 1] if pre_max[i] > suf_min else pre_max[i]
suf_min = min(suf_min, nums[i])
return ans
// Accepted solution for LeetCode #3660: Jump Game IX
// 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 #3660: Jump Game IX
// class Solution {
// public int[] maxValue(int[] nums) {
// int n = nums.length;
// int[] ans = new int[n];
// int[] preMax = new int[n];
// preMax[0] = nums[0];
// for (int i = 1; i < n; ++i) {
// preMax[i] = Math.max(preMax[i - 1], nums[i]);
// }
// int sufMin = 1 << 30;
// for (int i = n - 1; i >= 0; --i) {
// ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i];
// sufMin = Math.min(sufMin, nums[i]);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3660: Jump Game IX
function maxValue(nums: number[]): number[] {
const n = nums.length;
const ans = Array(n).fill(0);
const preMax = Array(n).fill(nums[0]);
for (let i = 1; i < n; i++) {
preMax[i] = Math.max(preMax[i - 1], nums[i]);
}
let sufMin = 1 << 30;
for (let i = n - 1; i >= 0; i--) {
ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i];
sufMin = Math.min(sufMin, nums[i]);
}
return 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.