|
| 1 | +# 2770. Maximum Number of Jumps to Reach the Last Index |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Dynamic Programming. |
| 5 | +- Similar Questions: Jump Game II, Frog Jump, Jump Game III, Jump Game IV, Minimum Jumps to Reach Home, Jump Game VII. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given a **0-indexed** array `nums` of `n` integers and an integer `target`. |
| 10 | + |
| 11 | +You are initially positioned at index `0`. In one step, you can jump from index `i` to any index `j` such that: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- `0 <= i < j < n` |
| 16 | + |
| 17 | +- `-target <= nums[j] - nums[i] <= target` |
| 18 | + |
| 19 | + |
| 20 | +Return **the **maximum number of jumps** you can make to reach index** `n - 1`. |
| 21 | + |
| 22 | +If there is no way to reach index `n - 1`, return `-1`. |
| 23 | + |
| 24 | + |
| 25 | +Example 1: |
| 26 | + |
| 27 | +``` |
| 28 | +Input: nums = [1,3,6,4,1,2], target = 2 |
| 29 | +Output: 3 |
| 30 | +Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: |
| 31 | +- Jump from index 0 to index 1. |
| 32 | +- Jump from index 1 to index 3. |
| 33 | +- Jump from index 3 to index 5. |
| 34 | +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. |
| 35 | +``` |
| 36 | + |
| 37 | +Example 2: |
| 38 | + |
| 39 | +``` |
| 40 | +Input: nums = [1,3,6,4,1,2], target = 3 |
| 41 | +Output: 5 |
| 42 | +Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: |
| 43 | +- Jump from index 0 to index 1. |
| 44 | +- Jump from index 1 to index 2. |
| 45 | +- Jump from index 2 to index 3. |
| 46 | +- Jump from index 3 to index 4. |
| 47 | +- Jump from index 4 to index 5. |
| 48 | +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. |
| 49 | +``` |
| 50 | + |
| 51 | +Example 3: |
| 52 | + |
| 53 | +``` |
| 54 | +Input: nums = [1,3,6,4,1,2], target = 0 |
| 55 | +Output: -1 |
| 56 | +Explanation: It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1. |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | +**Constraints:** |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +- `2 <= nums.length == n <= 1000` |
| 65 | + |
| 66 | +- `-109 <= nums[i] <= 109` |
| 67 | + |
| 68 | +- `0 <= target <= 2 * 109` |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +## Solution |
| 73 | + |
| 74 | +```javascript |
| 75 | +/** |
| 76 | + * @param {number[]} nums |
| 77 | + * @param {number} target |
| 78 | + * @return {number} |
| 79 | + */ |
| 80 | +var maximumJumps = function(nums, target) { |
| 81 | + var dp = Array(nums.length); |
| 82 | + for (var i = nums.length - 1; i >= 0; i--) { |
| 83 | + dp[i] = i === nums.length - 1 ? 0 : -1; |
| 84 | + for (var j = i + 1; j < nums.length; j++) { |
| 85 | + if (Math.abs(nums[j] - nums[i]) <= target && dp[j] !== -1) { |
| 86 | + dp[i] = Math.max(dp[i], 1 + dp[j]); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return dp[0]; |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +**Explain:** |
| 95 | + |
| 96 | +Bottom-up dynamic programming. |
| 97 | + |
| 98 | +**Complexity:** |
| 99 | + |
| 100 | +* Time complexity : O(n ^ 2). |
| 101 | +* Space complexity : O(n). |
0 commit comments