You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
Example 1:
Input: nums = [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
Constraints:
1 <= nums.length <= 1040 <= nums[i] <= 105When 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 standing at the starting point of a path made of stepping stones, and each stone allows you to take a certain number of forward steps. The brute force approach involves trying every single possible sequence of jumps to see if any of them can successfully lead you all the way to the final stone.
Here's how the algorithm would work step-by-step:
def can_jump(stepping_stones):
target_destination = len(stepping_stones) - 1
def explore_paths(current_position):
if current_position >= target_destination:
return True
maximum_steps = stepping_stones[current_position]
if maximum_steps == 0:
return False
# We branch out into every possible jump size to exhaustively check all pathways
for step_size in range(1, maximum_steps + 1):
next_position = current_position + step_size
if explore_paths(next_position):
return True
# If all branch attempts fail, this path is a dead end and we must backtrack
return False
# We initiate the recursive exploration starting from the very first stepping stone
return explore_paths(0)Instead of trying to map out every single path forward, the optimal strategy works backwards or keeps track of the furthest possible reach from the very beginning. By constantly updating how far you can travel based on your current standing, you can instantly tell if the final destination is within your maximum possible range.
Here's how the algorithm would work step-by-step:
def can_jump(jump_lengths): maximum_reachable_index = 0 for current_index, jump_length in enumerate(jump_lengths): # If we cannot reach the current stone, we are permanently stuck and cannot proceed further. if current_index > maximum_reachable_index: return False # Continuously update the furthest possible index we can ever reach from our current standing. potential_reach = current_index + jump_length if potential_reach > maximum_reachable_index: maximum_reachable_index = potential_reach # If our furthest reach extends to or beyond the final destination, we have won. if maximum_reachable_index >= len(jump_lengths) - 1: return True return maximum_reachable_index >= len(jump_lengths) - 1| Case | How to Handle |
|---|---|
| Single-element array | The greedy tracker initializes at index 0 and immediately reaches the final index, returning true. |
| Array starting with a zero | The maximum reach remains 0 at index 0, preventing any forward movement and returning false. |
| Array with zeros blocking the path | The greedy algorithm checks if the current index exceeds the maximum reachable distance, returning false when trapped. |
| Extremely large jump values at the beginning | The maximum reach extends beyond the final index, ensuring the loop condition satisfies early and returns true. |
| Strictly decreasing jumps of one | The reach increments by exactly one at each step, allowing the traversal to barely reach the final index. |
| Maximum-sized inputs up to constraint limits | A single linear scan runs in O(n) time and O(1) space, easily scaling without exceeding memory or time limits. |
| Array elements with large integer values | Reach calculations are capped at the array length to prevent unnecessary iterations or integer overflow issues. |