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
.
For example, if nums = [1,3,6,4,1,2]
and target = 2
, the output is 3
. To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
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.
## Maximum Number of Jumps
This problem asks us to find the maximum number of jumps we can make from index 0 to index `n-1` in a given array `nums`, subject to a constraint on the difference between the values at the jump indices. If it's impossible to reach the end, we should return -1.
### 1. Brute Force Solution
A straightforward approach is to use recursion to explore all possible jump sequences. Starting from index 0, we can jump to any subsequent index `j` if the condition `-target <= nums[j] - nums[i] <= target` is met. We recursively call the function for each valid jump and keep track of the maximum number of jumps.
```python
def max_jumps_recursive(nums, target, current_index):
n = len(nums)
if current_index == n - 1:
return 0 # Reached the end
max_jumps = -1 # Initialize with -1 (no path)
for next_index in range(current_index + 1, n):
if -target <= nums[next_index] - nums[current_index] <= target:
jumps = max_jumps_recursive(nums, target, next_index)
if jumps != -1:
max_jumps = max(max_jumps, 1 + jumps)
return max_jumps
def maxJumps_brute_force(nums, target):
return max_jumps_recursive(nums, target, 0)
# Example usage:
nums = [1, 3, 6, 4, 1, 2]
target = 2
result = maxJumps_brute_force(nums, target)
print(f"Maximum jumps (brute force): {result}") # Output: 3
This brute-force approach has exponential time complexity because it explores all possible jump combinations. This will lead to Time Limit Exceeded (TLE) for larger inputs.
A more efficient solution is to use dynamic programming. We can create a dp
array where dp[i]
stores the maximum number of jumps to reach index i
. We initialize dp[0]
to 0 because we start at index 0. For all other indices, we initialize dp[i]
to -1, indicating that they are initially unreachable.
We iterate through the array from left to right. For each index i
, if dp[i]
is not -1 (meaning we can reach index i
), we iterate through all subsequent indices j
and check if we can jump from i
to j
. If we can, we update dp[j]
with max(dp[j], dp[i] + 1)
. Finally, we return dp[n-1]
.
def maxJumps(nums, target):
n = len(nums)
dp = [-1] * n
dp[0] = 0 # Base case: 0 jumps to reach index 0
for i in range(n):
if dp[i] != -1:
for j in range(i + 1, n):
if -target <= nums[j] - nums[i] <= target:
dp[j] = max(dp[j], dp[i] + 1)
return dp[n - 1]
# Example usage:
nums = [1, 3, 6, 4, 1, 2]
target = 2
result = maxJumps(nums, target)
print(f"Maximum jumps (DP): {result}") # Output: 3
nums = [1, 3, 6, 4, 1, 2]
target = 3
result = maxJumps(nums, target)
print(f"Maximum jumps (DP): {result}") # Output: 5
nums = [1, 3, 6, 4, 1, 2]
target = 0
result = maxJumps(nums, target)
print(f"Maximum jumps (DP): {result}") # Output: -1
The dynamic programming solution has a time complexity of O(n^2), where n is the length of the nums
array. This is because we have a nested loop: the outer loop iterates through each index i
from 0 to n-1
, and the inner loop iterates through all subsequent indices j
from i+1
to n-1
. In the worst case, the inner loop runs for each element in the outer loop, leading to a quadratic time complexity.
The space complexity of the dynamic programming solution is O(n), where n is the length of the nums
array. This is because we use a dp
array of size n
to store the maximum number of jumps to reach each index. The dp
array is the dominant factor in terms of space usage.
nums
is empty or has only one element, there is no way to make any jumps to reach the end, so we should return -1.target
is 0, it means nums[j]
must be equal to nums[i]
for a jump to be valid. If no such jumps are possible, we will return -1.dp
array will remain -1.-target <= nums[j] - nums[i] <= target
condition.nums
array, then there are many possible paths and the solution should be able to handle that without errors.