2770. Maximum Number of Jumps to Reach the Last Index
Difficulty: Medium
Topics: Senior, Array, Dynamic Programming, Weekly Contest 353
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.
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-10⁹ <= nums[i] <= 10⁹0 <= target <= 2 * 10⁹
Hint:
- Use a dynamic programming approach.
- Define a dynamic programming array
dpof sizen, wheredp[i]represents the maximum number of jumps from index0to indexi. - For each j iterate over all
i < j. Setdp[j] = max(dp[j], dp[i] + 1)if-target <= nums[j] - nums[i] <= target.
Solution:
The solution uses dynamic programming to find the maximum number of jumps from index 0 to index n-1 in the array nums, where each jump from i to j must satisfy -target ≤ nums[j] − nums[i] ≤ target.
If the last index is unreachable, return -1. The DP array tracks the maximum jumps possible to reach each index, and the answer is the value stored at the last index.
Approach:
-
Dynamic Programming Array: Create a
dparray of sizenwheredp[i]stores the maximum number of jumps to reach indexifrom index0. Initializedp[0] = 0and all others to-inf(orPHP_INT_MIN). -
Transition: For each
jfrom1ton-1, check alli < j. If the difference betweennums[j]andnums[i]is within[-target, target]anddp[i]is not-inf, updatedp[j]asmax(dp[j], dp[i] + 1). -
Result: If
dp[n-1]is still-inf, return-1. Otherwise, returndp[n-1].
Let's implement this solution in PHP: 2770. Maximum Number of Jumps to Reach the Last Index
<?php
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
function maximumJumps(array $nums, int $target): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo maximumJumps([1,3,6,4,1,2], 2) . "\n"; // Output: 3
echo maximumJumps([1,3,6,4,1,2], 3) . "\n"; // Output: 5
echo maximumJumps([1,3,6,4,1,2], 0) . "\n"; // Output: -1
?>
Explanation:
-
Initialization:
dp[0] = 0because we start at index 0 with 0 jumps. All other indices are initially unreachable (PHP_INT_MIN). -
Outer loop (j): Iterates over each target index
jfrom1ton-1to compute the best way to reachj. -
Inner loop (i): Checks all previous indices
ifrom0toj-1to see if a valid jump fromitojexists. -
Valid jump condition:
-target <= nums[j] - nums[i] <= targetensures the jump is allowed. -
State update: If
iis reachable (dp[i] != PHP_INT_MIN), thendp[j] = max(dp[j], dp[i] + 1)means we can extend the path ending atitojwith one more jump. -
Why this works: The DP ensures that
dp[j]always holds the maximum jumps tojbecause we consider all possiblei < jthat can jump tojand keep the best value. -
Return: If
dp[n-1]is still-inf, no sequence exists → return-1. Otherwise, return the computed maximum jumps.
Complexity
-
Time Complexity: O(n²), because we have two nested loops iterating over
nelements each. - Space Complexity: O(n), for the DP array.
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)