|
| 1 | +<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> |
| 2 | + |
| 3 | +<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> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>0 <= i < j < n</code></li> |
| 7 | + <li><code>-target <= nums[j] - nums[i] <= target</code></li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return <em>the <strong>maximum number of jumps</strong> you can make to reach index</em> <code>n - 1</code>.</p> |
| 11 | + |
| 12 | +<p>If there is no way to reach index <code>n - 1</code>, return <code>-1</code>.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<pre> |
| 18 | +<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 2 |
| 19 | +<strong>Output:</strong> 3 |
| 20 | +<strong>Explanation:</strong> To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: |
| 21 | +- Jump from index 0 to index 1. |
| 22 | +- Jump from index 1 to index 3. |
| 23 | +- Jump from index 3 to index 5. |
| 24 | +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. </pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 3 |
| 30 | +<strong>Output:</strong> 5 |
| 31 | +<strong>Explanation:</strong> To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: |
| 32 | +- Jump from index 0 to index 1. |
| 33 | +- Jump from index 1 to index 2. |
| 34 | +- Jump from index 2 to index 3. |
| 35 | +- Jump from index 3 to index 4. |
| 36 | +- Jump from index 4 to index 5. |
| 37 | +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. </pre> |
| 38 | + |
| 39 | +<p><strong class="example">Example 3:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 0 |
| 43 | +<strong>Output:</strong> -1 |
| 44 | +<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. |
| 45 | +</pre> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | +<p><strong>Constraints:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><code>2 <= nums.length == n <= 1000</code></li> |
| 52 | + <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> |
| 53 | + <li><code>0 <= target <= 2 * 10<sup>9</sup></code></li> |
| 54 | +</ul> |
0 commit comments