1340. Jump Game V
Difficulty: Hard
Topics: Senior Staff, Array, Dynamic Programming, Sorting, Weekly Contest 174
Given an array of integers arr and an integer d. In one step you can jump from index i to index:
-
i + xwhere:i + x < arr.lengthand0 < x <= d. -
i - xwhere:i - x >= 0and0 < 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 --> 7as shown.- Note that if you start at index
6you can only jump to index7. You cannot jump to index5because13 > 9. You cannot jump to index4because index5is between index4and6and13 > 9. - Similarly, You cannot jump from index
3to index2or index1.
- Note that if you start at index
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.
Example 4:
- Input: arr = [1], d = 1
- Output: 1
Example 5:
- Input: arr = [10,5,1,9,2], d = 4
- Output: 4
Example 6:
- Input: arr = [5,3,7,2,4,1], d = 2
- Output: 3
Constraints:
1 <= arr.length <= 10001 <= arr[i] <= 10⁵1 <= d <= arr.length
Hint:
- Use dynamic programming.
dp[i]is max jumps you can do starting from indexi. Answer ismax(dp[i]). -
dp[i] = 1 + max (dp[j])wherejis all indices you can reach fromi.
Solution:
This solution solves the Jump Game V problem using Dynamic Programming with Memoization and Precomputed Reachability. The key insight is that for each index, we can only jump to lower-valued indices within distance d where all intermediate heights are lower than the starting point. The algorithm precomputes all valid jumps from each position and then uses DFS with memoization to find the maximum path length starting from any index.
Approach:
-
Precomputation of Reachable Indices: For each index
i, scan left and right up to distanced, adding indicesjwhere all heights betweeniandjare strictly less thanarr[i] -
Early Termination: Stop scanning in a direction when encountering a value ≥
arr[i], as no further jumps are possible in that direction - Memoized DFS: Use depth-first search with memoization to compute the maximum jump count from each index without redundant calculations
- Bottom-up Dynamic Programming Concept: The recursive approach with memoization is equivalent to building DP from the smallest values upward
Let's implement this solution in PHP: 1340. Jump Game V
<?php
/**
* @param Integer[] $arr
* @param Integer $d
* @return Integer
*/
function maxJumps(array $arr, int $d): int
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $i
* @param $dp
* @param $reachable
* @return int|mixed
*/
function dfs($i, &$dp, &$reachable): mixed
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo maxJumps([6,4,14,6,8,13,9,7,10,6,12], 2) . "\n"; // Output: 4
echo maxJumps([3,3,3,3,3], 3) . "\n"; // Output: 1
echo maxJumps([7,6,5,4,3,2,1], 1) . "\n"; // Output: 7
echo maxJumps([1], 1) . "\n"; // Output: 1
echo maxJumps([10,5,1,9,2], 4) . "\n"; // Output: 4
echo maxJumps([5,3,7,2,4,1], 2) . "\n"; // Output: 3
?>
Explanation:
-
Precomputation Phase: For each index, two loops run—one leftwards and one rightwards—stopping when a higher or equal value blocks further jumps or distance limit
dis reached -
Reachability Storage: Stores valid jump targets in
$reachable[$i]array, allowing O(1) access to all positions reachable fromi -
DFS with Memoization: Function
dfs($i, &$dp, &$reachable)recursively explores all reachable positions, caching results in$dparray - Base Case: When no jumps are possible from a position, it returns 1 (only the current index)
-
Optimal Substructure: The maximum path from index
iis 1 plus the maximum of all maximum paths from its reachable indices - Answer Aggregation: Iterate through all starting indices to find the global maximum
Complexity
-
Time Complexity:
-
O(n × d) where
nis array length and d is maximum jump distance - Precomputation scans at most
2dindices per position: O(2n×d) = O(n×d) - DFS visits each index once due to memoization: O(n)
- Total: O(n×d)
-
O(n × d) where
-
Space Complexity:
- O(n×d) in worst case
- Reachability array stores at most O(n×d) references
- Memoization array: O(n)
- Recursion stack: O(n) in worst case
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Top comments (0)