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 < nReturn 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 <= 1040 <= nums[i] <= 1000nums[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:
Imagine you are trying to cross a path of stepping stones where each stone tells you the maximum distance you are allowed to jump forward. The brute force approach involves trying every single possible combination of jumps to see which path gets you to the end using the fewest steps.
Here's how the algorithm would work step-by-step:
def jump_game_brute_force(array_of_numbers):
def explore_paths(current_position, jump_count):
# Stop exploring this branch if we have already reached or passed the final destination
if current_position >= len(array_of_numbers) - 1:
return jump_count
max_reach = array_of_numbers[current_position]
if max_reach == 0:
return float('inf')
minimum_jumps = float('inf')
# Try every single possible jump distance from the current stone to explore all paths
for next_step in range(1, max_reach + 1):
result = explore_paths(current_position + next_step, jump_count + 1)
if result < minimum_jumps:
minimum_jumps = result
return minimum_jumps
# Guard against empty arrays or trivial paths where no movement is required
if len(array_of_numbers) <= 1:
return 0
total_minimum_jumps = explore_paths(0, 0)
# Ensure we return zero instead of infinity if the target is completely unreachable
if total_minimum_jumps == float('inf'):
return 0
return total_minimum_jumpsInstead of testing every possible sequence of jumps, the optimal strategy treats the journey like a series of checkpoints organized by distance. By looking at how far you can reach from your current stopping points, you plan your next mandatory stop only when you absolutely have to.
Here's how the algorithm would work step-by-step:
def jump(array_of_nums): if len(array_of_nums) <= 1: return 0 jump_count = 0 current_furthest_reach = 0 next_furthest_reach = 0 # We evaluate all reachable indices within our current range before committing to a jump. for current_index in range(len(array_of_nums) - 1): next_furthest_reach = max(next_furthest_reach, current_index + array_of_nums[current_index]) # A jump is forced only when we exhaust the boundary of our current driving range. if current_index == current_furthest_reach: jump_count += 1 current_furthest_reach = next_furthest_reach # We terminate early if our current reach already covers or passes the final destination. if current_furthest_reach >= len(array_of_nums) - 1: break return jump_count| Case | How to Handle |
|---|---|
| Array with a single element | Return zero jumps immediately because we are already standing at the final destination. |
| Array starting with a zero where the second element is non-zero | Start at index zero, take one jump using the first element's capacity, and continue the greedy range expansion. |
| Array where the first element can jump directly to the last index | The greedy algorithm will evaluate the max reach, see it covers the last index, and return exactly one jump. |
| Array containing consecutive zeros blocking progress | Although the problem statement guarantees the last index is reachable, the greedy tracker naturally maintains the furthest possible reach before dead ends. |
| Extremely large arrays up to the maximum constraints | The linear time greedy approach processes the array in a single pass without recursion overhead, preventing stack overflows and ensuring optimal scalability. |
| Array with all elements equal to one | The algorithm increments the jump counter at every single step, correctly returning a total of n minus one jumps for an array of size n. |
| Array with decreasing or strictly increasing jump values | The greedy window dynamically updates the farthest reachable index at each step regardless of the local value distribution. |
| Input values near the maximum integer limit | The jump reach calculations are bounded within the array length constraints, avoiding any potential integer overflow issues. |