You are given a 0-indexed array of integers nums
of length n
. You are initially positioned at nums[0]
.
Each element nums[i]
represents the maximum length of a forward jump from index i
. In other words, if you are at nums[i]
, you can jump to any nums[i + j]
where:
0 <= j <= nums[i]
andi + j < n
Return the minimum number of jumps to reach nums[n - 1]
. The test cases are generated such that you can reach nums[n - 1]
.
Example 1:
Input: nums = [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [2,3,0,1,4] Output: 2
Constraints:
1 <= nums.length <= 104
0 <= nums[i] <= 1000
nums[n - 1]
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the fewest jumps needed to reach the end. The brute force method tries every single possible path of jumps, exploring all combinations to see which one reaches the end with the smallest number of jumps.
Here's how the algorithm would work step-by-step:
def jump_game_brute_force(numbers):
if not numbers:
return 0
number_of_jumps = float('inf')
def solve_jump_game(current_index, jumps_taken):
nonlocal number_of_jumps
# Found a path to the end, record jumps
if current_index >= len(numbers) - 1:
number_of_jumps = min(number_of_jumps, jumps_taken)
return
maximum_reach = numbers[current_index]
# If we can't jump, this path is invalid
if maximum_reach == 0:
return
# Iterate through all possible jump lengths
for jump_length in range(1, maximum_reach + 1):
next_index = current_index + jump_length
solve_jump_game(next_index, jumps_taken + 1)
# Start exploring from the begining
solve_jump_game(0, 0)
if number_of_jumps == float('inf'):
return -1
else:
return number_of_jumps
The goal is to reach the end using the fewest jumps possible. We don't need to find every possible path; we just need to greedily select the jump that gets us furthest along at each step.
Here's how the algorithm would work step-by-step:
def jump_game_two(nums):
array_length = len(nums)
current_reach = 0
next_reach = 0
jumps = 0
for i in range(array_length - 1):
# Update how far we can reach
next_reach = max(next_reach, i + nums[i])
if i == current_reach:
# Need to jump because we've reached the limit
jumps += 1
current_reach = next_reach
# If we can't reach further, impossible to finish
if current_reach <= i:
return -1
return jumps
Case | How to Handle |
---|---|
Null or empty input array | Return 0 if the array is null or empty, as no jumps are needed. |
Array with a single element | Return 0 if the array has only one element because you start at the first index. |
First element is zero, and array length is greater than 1 | Return -1 (or throw an exception) because it's impossible to move from the first position. |
Array where it's impossible to reach the last index | Return -1 if the last index cannot be reached. |
Array with all 1s | Handle correctly as each jump moves one step forward. |
Large jump lengths that could potentially cause integer overflow in intermediate calculations. | Ensure intermediate calculations use appropriate data types to prevent integer overflow. |
Very large input array size | The solution should be designed to scale well and avoid excessive memory usage for large inputs (e.g., O(n) space complexity). |
Input array with very large numbers, close to integer max | The algorithm should handle large numbers without causing overflow issues in calculations. |