KatInterview

403. Frog Jump

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks if a frog can cross a river by hopping from stone to stone. The frog starts at the first stone (position 0) and must reach the last stone. The key constraint is on the jump lengths: if the previous jump was k units, the next jump must be k-1, k, or k+1 units. This problem can be modeled as finding a path in a graph, where states are defined not just by the frog's position, but also by the size of its last jump. This structure makes it a great candidate for Dynamic Programming or Breadth-First Search.

Solution

Frog Jump in Python

class Solution:
    def canCross(self, stones: List[int]) -> bool:
        n = len(stones)
        # map: stone_position -> set of jump sizes to reach this stone
        jumps = {stone: set() for stone in stones}
        
        # Initially at stone 0 with a jump of size 0
        jumps[0].add(0)

        for stone in stones:
            # For each jump size k that can reach the current stone
            for k in list(jumps[stone]): # Iterate over a copy as the set might be modified
                # Try next jumps of size k-1, k, k+1
                for step in range(k - 1, k + 2):
                    # Jump must be positive (forward)
                    if step > 0:
                        next_stone = stone + step
                        # If we can reach the last stone, we are done
                        if next_stone == stones[-1]:
                            return True
                        # If a stone exists at the next position
                        if next_stone in jumps:
                            jumps[next_stone].add(step)
        
        # If the loop finishes, it means the last stone was never reached
        return False

Frog Jump in Java

class Solution {
    public boolean canCross(int[] stones) {
        int n = stones.length;
        // map: stone_position -> set of jump sizes to reach this stone
        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
        for (int stone : stones) {
            map.put(stone, new HashSet<>());
        }

        // Initially at stone 0 with a jump of size 0
        map.get(0).add(0);

        for (int currentStone : stones) {
            // For each jump size k that can reach the current stone
            for (int k : map.get(currentStone)) {
                // Try next jumps of size k-1, k, k+1
                for (int step = k - 1; step <= k + 1; step++) {
                    // Jump must be positive (forward)
                    if (step > 0) {
                        long nextStonePos = (long)currentStone + step;
                        // If we can reach the last stone, we are done
                        if (nextStonePos == stones[n - 1]) {
                            return true;
                        }
                        // If a stone exists at the next position, check if it's in the map
                        // Using int conversion as map keys are Integer, but positions fit
                        if (map.containsKey((int)nextStonePos)) {
                            map.get((int)nextStonePos).add(step);
                        }
                    }
                }
            }
        }
        
        // If the loop finishes, it means the last stone was never reached
        return false;
    }
}

Frog Jump in C++

class Solution {
public:
    bool canCross(vector<int>& stones) {
        int n = stones.size();
        // map: stone_position -> set of jump sizes to reach this stone
        unordered_map<int, unordered_set<int>> jumps;
        for (int stone : stones) {
            jumps[stone] = unordered_set<int>();
        }

        // Initially at stone 0 with a jump of size 0
        jumps[0].insert(0);

        for (int current_stone : stones) {
            // For each jump size k that can reach the current stone
            for (int k : jumps[current_stone]) {
                // Try next jumps of size k-1, k, k+1
                for (int step = k - 1; step <= k + 1; ++step) {
                    // Jump must be positive (forward)
                    if (step > 0) {
                        long next_stone_pos = (long)current_stone + step;
                        // If we can reach the last stone, we are done
                        if (next_stone_pos == stones[n - 1]) {
                            return true;
                        }
                        // If a stone exists at the next position
                        if (jumps.count(next_stone_pos)) {
                            jumps[next_stone_pos].insert(step);
                        }
                    }
                }
            }
        }
        
        // If the loop finishes, it means the last stone was never reached
        return false;
    }
};

Frog Jump in TypeScript

function canCross(stones: number[]): boolean {
    const n = stones.length;
    // map: stone_position -> set of jump sizes to reach this stone
    const jumps = new Map<number, Set<number>>();
    for (const stone of stones) {
        jumps.set(stone, new Set());
    }

    // Initially at stone 0 with a jump of size 0
    jumps.get(0)!.add(0);

    for (const stone of stones) {
        // For each jump size k that can reach the current stone
        for (const k of jumps.get(stone)!) {
            // Try next jumps of size k-1, k, k+1
            for (let step = k - 1; step <= k + 1; step++) {
                // Jump must be positive (forward)
                if (step > 0) {
                    const nextStone = stone + step;
                    // If we can reach the last stone, we are done
                    if (nextStone === stones[n - 1]) {
                        return true;
                    }
                    // If a stone exists at the next position
                    if (jumps.has(nextStone)) {
                        jumps.get(nextStone)!.add(step);
                    }
                }
            }
        }
    }
    
    // If the loop finishes, it means the last stone was never reached
    return false;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.