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 <= 104
0 <= nums[i] <= 105
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 brute force strategy for this game involves trying every possible path you could take. We essentially explore all possible jump sequences, even if they seem silly, until we find one that works.
Here's how the algorithm would work step-by-step:
def can_jump_brute_force(jump_lengths):
def solve(current_position):
# If we've reached the end, we're done
if current_position >= len(jump_lengths) - 1:
return True
maximum_jump = jump_lengths[current_position]
# Iterate through all possible jump lengths
for jump_length in range(1, maximum_jump + 1):
next_position = current_position + jump_length
# Explore the result of taking this jump
if solve(next_position):
return True
# We've explored all jumps from here, and none work
return False
# Start from the beginning
return solve(0)
The problem asks if you can reach the end, given you can jump a certain distance from each position. Instead of trying to jump every possible distance at each spot, the efficient approach determines how far you *need* to jump to reach the end.
Here's how the algorithm would work step-by-step:
def can_jump_to_end(jump_lengths):
destination_index = len(jump_lengths) - 1
for i in range(len(jump_lengths) - 2, -1, -1):
# Check if we can reach the current destination from this position
if i + jump_lengths[i] >= destination_index:
#If we can, update the destination to the current position.
destination_index = i
#If first position is the destination, we can reach the end.
if destination_index == 0:
return True
return False
Case | How to Handle |
---|---|
Null or empty input array | Return true if the input is null or empty because an empty array trivially allows reaching the end (which is the start). |
Single element array | Return true as a single-element array always allows reaching the end. |
Array where the first element is zero | If the first element is zero and the array has more than one element, return false immediately because we cannot move from the starting position. |
Array where all elements are zero (except perhaps the last) | Iterate to see if any position can reach beyond it's index, return false if we get stuck. |
Array where early elements have large jump values, but later elements are mostly zeros | The algorithm needs to correctly handle scenarios where the large jumps are wasted if the later jumps cannot sustain the progress. |
Array with a very long sequence of zeros preventing forward progress | The solution should be able to identify a sequence of zeros that prevents reaching the end, resulting in returning false. |
Integer overflow if indices are large or number of jumps is large | The algorithm shouldn't perform computations with indices which could cause int overflow. |
Large array size impacting performance | The algorithm should scale linearly O(n) with array size and should avoid recursive implementations that cause a stack overflow. |