Input: nums =[1,3,6,4,1,2], target =2Output: 3Explanation: To go from index 0 to index n -1with 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 -1with more than 3 jumps. Hence, the answer is3.
Input: nums =[1,3,6,4,1,2], target =3Output: 5Explanation: To go from index 0 to index n -1with 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 -1with more than 5 jumps. Hence, the answer is5.
Input: nums =[1,3,6,4,1,2], target =0Output: -1Explanation: It can be proven that there is no jumping sequence that goes from 0 to n -1. Hence, the answer is-1.
We want to maximize the number of jumps to reach the last index, where each jump from i to j is allowed if the difference between nums[j] and nums[i] is within [-target, target]. We can use dynamic programming to track the maximum jumps to each index.