A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of stones positions (in units) in sorted ascending order, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be 1 unit.
If the frog's last jump was k units, its next jump must be either k - 1, k, or k + 1 units. The frog can only jump in the forward direction.
Example 1:
Input: stones = [0,1,3,5,6,8,12,17] Output: true Explanation: The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone.
Example 2:
Input: stones = [0,1,2,3,4,8,9,11] Output: false Explanation: There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large.
Constraints:
2 <= stones.length <= 20000 <= stones[i] <= 231 - 1stones[0] == 0stones is sorted in a strictly increasing order.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 a frog trying to cross a river by hopping from stone to stone, where each hop can be slightly different in size. The brute force approach involves exploring every single path of hops the frog could possibly take until it either gets stuck or reaches the final stone.
Here's how the algorithm would work step-by-step:
class Solution:
def canCross(self, stones: list[int]) -> bool:
stone_positions = set(stones)
final_stone = stones[-1]
# We memoize paths to prevent redundant recursive calculations on dead ends
memoization_cache = set()
def recursive_depth_first_search(current_position, current_jump_size):
if current_jump_size <= 0:
return False
if current_position == final_stone:
return True
state_identifier = (current_position, current_jump_size)
if state_identifier in memoization_cache:
return False
# We test all three variation paths for the next hop size rule
for next_jump_size in (current_jump_size - 1, current_jump_size, current_jump_size + 1):
if next_jump_size > 0:
next_position = current_position + next_jump_size
if next_position in stone_positions:
if recursive_depth_first_search(next_position, next_jump_size):
return True
# We cache failed states so we never re-evaluate identical dead ends
memoization_cache.add(state_identifier)
return False
if len(stones) > 1 and stones[1] != 1:
return False
return recursive_depth_first_search(0, 0)Instead of blindly testing every possible sequence of jumps, the optimal strategy remembers the exact size of the previous jump made at each resting spot. By keeping track of what previous jump sizes are possible at every location, the strategy avoids repeating work and quickly determines if the final destination is reachable.
Here's how the algorithm would work step-by-step:
class Solution:
def canCross(self, stones: list[int]) -> bool:
stone_positions = {stone: index for index, stone in enumerate(stones)}
jump_tracking_map = {stone: set() for stone in stones}
jump_tracking_map[stones[0]].add(0)
# We iterate through each stone to evaluate possible forward jumps from current positions.
for stone_index, current_stone in enumerate(stones):
for previous_jump_size in jump_tracking_map[current_stone]:
for delta_adjustment in [-1, 0, 1]:
next_jump_size = previous_jump_size + delta_adjustment
if next_jump_size > 0:
target_stone = current_stone + next_jump_size
if target_stone in stone_positions:
jump_tracking_map[target_stone].add(next_jump_size)
# We check if any valid jump sequence successfully reached the final destination stone.
return len(jump_tracking_map[stones[-1]]) > 0| Case | How to Handle |
|---|---|
| Input array is empty or null | Return false immediately as there are no stones to cross. |
| Input array has only one element | Return true immediately since the frog starts on the final stone. |
| The second stone is not at position 1 | Return false because the frog's first mandatory jump size of 1 cannot land on the second stone. |
| Large input size with maximum possible stone positions | Use memoized dynamic programming or a hash set to avoid exponential time complexity and prevent stack overflow. |
| Gaps between stones are too large for any valid jump sequence | The algorithm will exhaust all valid jump branches and correctly return false. |
| Stones are densely packed with multiple valid paths to the end | Memoization ensures overlapping subproblems are computed only once, maintaining optimal efficiency. |
| Large jump distances causing integer overflow | Use standard 32-bit or 64-bit signed integers since stone positions fit well within standard integer limits. |
| Input array has duplicate stone positions | Store valid jump states per stone index in a hash map to correctly process multiple identical positions. |