2770. Maximum Number of Jumps to Reach the Last Index
SourceWeekly Contest 353 Q2DifficultyMediumRating1533
Description
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-109 <= nums[i] <= 1090 <= target <= 2 * 109
Solutions
Solution 1: Memoization
Thinking
Jump from index \(0\) to the end, each step requiring an absolute difference at most \(\textit{target}\), and maximize the number of jumps. \(n\le 1000\); a shortest-path formulation would minimize jumps instead.
\(\textit{dfs}(i)\) is the most jumps from \(i\): take \(1+\textit{dfs}(j)\) over legal \(j>i\), \(0\) at the end, and \(-\infty\) when no jump exists. If the memoized value is negative, return \(-1\).
For each position \(i\), we consider to jump to position \(j\) which satisfies \(|\textit{nums}[i] - \textit{nums}[j]| \leq \textit{target}\). Then we can jump from \(i\) to \(j\), and continue to jump from \(j\) to the end.
Therefore, we design a function \(\textit{dfs}(i)\), which represents the maximum number of jumps needed to jump to the end index starting from position \(i\). Then the answer is \(\textit{dfs}(0)\).
The calculation process of function \(\textit{dfs}(i)\) is as follows:
- If \(i = n - 1\), then we have reached the end index and no jumps are required, so return \(0\);
- Otherwise, we need to enumerate the positions \(j\) that can be jumped from position \(i\), and calculate the maximum number of jumps needed to jump to the end index starting from \(j\), then \(\textit{dfs}(i)\) is equal to the maximum value of all \(\textit{dfs}(j)\) plus \(1\). If there is no position \(j\) that can be jumped from \(i\), then \(\textit{dfs}(i) = -\infty\).
To avoid duplicate calculations, we can use memoization.
The time complexity is \(O(n^2)\), and the space complexity is \(O(n)\), where \(n\) is the length of array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Solution 2: Dynamic Programming
Thinking
Enumerating every jump sequence is exponential. The array length reaches \(1000\), and a legal step to the next index is always tried first, so the search has depth \(n\). The most jumps from index \(i\) depend only on later indexes. Let \(f[i]\) be that number, with \(f[n-1]=0\), and scan landing indexes from the right, updating with \(1+f[j]\) when the absolute difference is at most \(\textit{target}\). A still-negative \(f[0]\) means the end is unreachable, so the answer is \(-1\).
Let \(f[i]\) be the maximum number of jumps from index \(i\) to the end. Set \(f[n-1]=0\) and every earlier entry to \(-\infty\). For \(i\) from \(n-2\) down to \(0\), enumerate a landing index \(j\) (\(i < j < n\)). If \(|\textit{nums}[i]-\textit{nums}[j]| \leq \textit{target}\), update \(f[i]\) with \(1+f[j]\). If \(f[0]\) is still negative, the end cannot be reached and the answer is \(-1\); otherwise the answer is \(f[0]\).
The time complexity is \(O(n^2)\), and the space complexity is \(O(n)\), where \(n\) is the length of array.
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |