Maximum Number of Jumps to Reach the Last Index

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
Inputnums = [1,3,6,4,1,2], target = 2
Output3
The maximum jumping sequence is from index 0 to 1, then 1 to 3, then 3 to 5, for 3 jumps.
Example 2
Inputnums = [1,3,6,4,1,2], target = 3
Output5
The maximum jumping sequence visits each next index from 0 through 5, for 5 jumps.

Constraints

  • 2 <= nums.length == n <= 1000
  • -10^9 <= nums[i] <= 10^9
  • 0 <= target <= 2 * 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate