DEV Community

Cover image for 2770. Maximum Number of Jumps to Reach the Last Index
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

2770. Maximum Number of Jumps to Reach the Last Index

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:

  1. Use a dynamic programming approach.
  2. Define a dynamic programming array dp of size n, where dp[i] represents the maximum number of jumps from index 0 to index i.
  3. For each j iterate over all i < j. Set dp[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 dp array of size n where dp[i] stores the maximum number of jumps to reach index i from index 0. Initialize dp[0] = 0 and all others to -inf (or PHP_INT_MIN).
  • Transition: For each j from 1 to n-1, check all i < j. If the difference between nums[j] and nums[i] is within [-target, target] and dp[i] is not -inf, update dp[j] as max(dp[j], dp[i] + 1).
  • Result: If dp[n-1] is still -inf, return -1. Otherwise, return dp[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
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Initialization: dp[0] = 0 because 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 j from 1 to n-1 to compute the best way to reach j.
  • Inner loop (i): Checks all previous indices i from 0 to j-1 to see if a valid jump from i to j exists.
  • Valid jump condition: -target <= nums[j] - nums[i] <= target ensures the jump is allowed.
  • State update: If i is reachable (dp[i] != PHP_INT_MIN), then dp[j] = max(dp[j], dp[i] + 1) means we can extend the path ending at i to j with one more jump.
  • Why this works: The DP ensures that dp[j] always holds the maximum jumps to j because we consider all possible i < j that can jump to j and 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 n elements 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!
Buy Me A Coffee

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

Top comments (0)