java.lang.Object
g2701_2800.s2770_maximum_number_of_jumps_to_reach_the_last_index.Solution

public class Solution extends Object
2770 - Maximum Number of Jumps to Reach the Last Index.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers and an integer <code>target</code>.</p> <p>You are initially positioned at index <code>0</code>. In one step, you can jump from index <code>i</code> to any index <code>j</code> such that:</p> <ul> <li><code>0 <= i < j < n</code></li> <li><code>-target <= nums[j] - nums[i] <= target</code></li> </ul> <p>Return <em>the <strong>maximum number of jumps</strong> you can make to reach index</em> <code>n - 1</code>.</p> <p>If there is no way to reach index <code>n - 1</code>, return <code>-1</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,3,6,4,1,2], target = 2</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong></p> <p>To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:</p> <ul> <li> <p>Jump from index 0 to index 1.</p> </li> <li> <p>Jump from index 1 to index 3.</p> </li> <li> <p>Jump from index 3 to index 5.</p> </li> </ul> <p>It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps.</p> <p>Hence, the answer is 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,3,6,4,1,2], target = 3</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong></p> <p>To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:</p> <ul> <li> <p>Jump from index 0 to index 1.</p> </li> <li> <p>Jump from index 1 to index 2.</p> </li> <li> <p>Jump from index 2 to index 3.</p> </li> <li> <p>Jump from index 3 to index 4.</p> </li> <li> <p>Jump from index 4 to index 5.</p> </li> </ul> <p>It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps.</p> <p>Hence, the answer is 5.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,3,6,4,1,2], target = 0</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= nums.length == n <= 1000</code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>0 <= target <= 2 * 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumJumps

      public int maximumJumps(int[] nums, int target)