Advertisement

Frog Jump - LeetCode 403 Solution

Frog Jump - Complete Solution Guide

Frog Jump is LeetCode problem 403, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 ,

Detailed Explanation

The Frog Jump problem asks us to determine if a frog can cross a river by jumping on stones. The river is divided into units, and stones are placed at certain units. The frog starts at the first stone (position 0) and must jump 1 unit to the second stone. Subsequent jumps can be of size k-1, k, or k+1, where k is the size of the previous jump. The goal is to determine if the frog can reach the last stone.

Solution Approach

The solution uses dynamic programming to track the possible jump sizes that can lead the frog to each stone. A hash map (dictionary in Python) is used where the key is the stone position and the value is a set of jump sizes that can reach that stone. The algorithm iterates through the stones. For each stone and each jump size that can reach it, it explores the next possible jumps (k-1, k, k+1) to see if they lead to other stones. If a jump lands on another stone, the corresponding jump size is added to the set of possible jump sizes for that stone. If the frog reaches the last stone, the function returns `true`. Otherwise, it returns `false` after exploring all possibilities.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash map (or dictionary) called `dp` to store the possible jump sizes for each stone. The key is the stone's position, and the value is a set of jump sizes.
  2. Step 2: Initialize the jump sizes for the first stone (position 0) to be {0}, since frog starts with a 'jump size' of zero at first position.
  3. Step 3: Iterate through the stones array.
  4. Step 4: For each stone, iterate through the set of jump sizes that can reach it.
  5. Step 5: For each jump size `k`, calculate the next possible positions by jumping `k-1`, `k`, and `k+1` units forward.
  6. Step 6: Check if these new positions exist as stones. If they do, add `k-1`, `k`, or `k+1` to the set of jump sizes for that stone (only if the jump size is > 0).
  7. Step 7: If any jump leads to the last stone, return `true`.
  8. Step 8: If, after iterating through all stones and jump sizes, the last stone is not reached, return `false`.

Key Insights

  • Insight 1: Dynamic programming is crucial. We need to keep track of which jump sizes can reach each stone, not just whether a stone can be reached.
  • Insight 2: Using a hash map (or dictionary) is effective for storing the mapping between stone positions and the possible jump sizes that can reach them.
  • Insight 3: Early exit condition. Check if the second stone exists and equals 1, because the frog's first jump must be 1. If the second stone is not 1 or doesn't exist, return false

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n^2)

Topics

This problem involves: Array, Dynamic Programming.

Companies

Asked at: LINE, Otter.ai, PhonePe, Snap, Zomato.