{x}
blog image

Frog Jump

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 <= 2000
  • 0 <= stones[i] <= 231 - 1
  • stones[0] == 0
  • stones is sorted in a strictly increasing order.

403. Frog Jump

This problem asks whether a frog can reach the last stone in a river, given a list of stone positions and jump constraints. The frog starts at the first stone (position 0) and must jump to the last stone. The jump distance must be one of: k-1, k, or k+1, where k is the distance of the previous jump.

Solution Approaches and Code

Two main approaches effectively solve this problem: Memoization with Depth-First Search (DFS) and Dynamic Programming. Both have a time complexity of O(n^2), where n is the number of stones.

1. Hash Table + Memoization (DFS)

This approach uses a hash table to store the index of each stone for quick lookup. The core is a recursive DFS function dfs(i, k):

  • dfs(i, k): Represents whether the frog can reach the last stone starting from stone i with the last jump being k.

  • Base Case: If i is the index of the last stone, return true (frog reached the end).

  • Recursive Step: Iterate through possible jump distances (k-1, k, k+1). Check if a stone exists at the calculated position (stones[i] + j). If it does, recursively call dfs for the new stone and jump distance. If the recursive call returns true, return true immediately (path found).

  • Memoization: A cache (f in the code) stores the results of dfs(i,k), preventing redundant computations and significantly improving performance.

Time Complexity: O(n^2) due to the nested loop in the DFS function. Memoization reduces the worst-case time.

Space Complexity: O(n^2) for the memoization table.

class Solution:
    def canCross(self, stones: List[int]) -> bool:
        @cache
        def dfs(i, k):
            if i == n - 1:
                return True
            for j in range(k - 1, k + 2):
                if j > 0 and stones[i] + j in pos and dfs(pos[stones[i] + j], j):
                    return True
            return False
 
        n = len(stones)
        pos = {s: i for i, s in enumerate(stones)}
        return dfs(0, 0)

(Java, C++, Go, TypeScript, and Rust implementations are similar in structure, using their respective memoization techniques and data structures.)

2. Dynamic Programming

This approach uses a 2D boolean array dp[i][k] where:

  • dp[i][k] is true if the frog can reach stone i with a last jump of size k.

The algorithm iterates through the stones and updates dp based on possible previous jump sizes. If dp[n-1][k] becomes true for any k, the frog can reach the last stone.

Time Complexity: O(n^2) due to the nested loops iterating through stones and jump sizes.

Space Complexity: O(n^2) for the dp array.

class Solution:
    def canCross(self, stones: List[int]) -> bool:
        n = len(stones)
        f = [[False] * n for _ in range(n)]
        f[0][0] = True
        for i in range(1, n):
            for j in range(i - 1, -1, -1):
                k = stones[i] - stones[j]
                if k - 1 > j:
                    break
                f[i][k] = f[j][k - 1] or f[j][k] or f[j][k + 1]
                if i == n - 1 and f[i][k]:
                    return True
        return False
 

(Similar Java, C++, Go, TypeScript, and Rust implementations exist, using their respective array and boolean handling.)

Summary

Both approaches provide correct solutions with the same time and space complexity. The choice depends on personal preference; the dynamic programming approach might be slightly easier to understand for some, while memoization can feel more intuitive to others. Both solutions are optimized to avoid redundant computations.