The Frog Jump coding problem describes a frog trying to cross a river by jumping from stone to stone. The frog starts at stone 0. If the frog's last jump was k units, its next jump must be k-1, k, or k+1 units. The first jump must be exactly 1 unit. You are given an array of stone positions, and you need to determine if the frog can reach the last stone.
This is a "Hard" Dynamic Programming problem asked by companies like Amazon, Meta, and Snap. It tests your ability to define a complex state. The state isn't just "which stone am I on?", but "which stone am I on AND what was my last jump size?". It evaluates your ability to use Hash Maps to store DP states efficiently.
This problem uses Dynamic Programming with Hash Maps.
map[stone] contains all the jump sizes that successfully brought the frog to that stone.map[0] = {0} (or handle the first jump manually).s.k in map[s], calculate the possible next jumps: k-1, k, k+1.j > 0, if s + j is a valid stone position in our map, add j to the set map[s + j].Stones: [0, 1, 3, 5, 6, 8, 12, 17]
map[1] gets jump 1.k=1. Next jumps: 0, 1, 2.
1+1=2 (Not a stone).1+2=3 (Is a stone). map[3] gets jump 2.k=2. Next jumps: 1, 2, 3.
3+2=5. map[5] gets jump 2.3+3=6. map[6] gets jump 3.
Continue this process. If 17 gets any jump value, return true.dp[i] = boolean, ignoring the fact that how you got to stone i dictates where you can go next.dp[stone_index][jump_size] can work, but a Hash Map of Sets is often more intuitive and handles sparse stones better.When the valid transitions from state A depend on the path taken to reach state A, your DP state must include that path information (in this case, the last_jump size).
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Best Time to Buy and Sell Stock III | Hard | Solve | |
| Burst Balloons | Hard | Solve | |
| Minimum Swaps To Make Sequences Increasing | Hard | Solve | |
| Best Time to Buy and Sell Stock IV | Hard | Solve | |
| Arithmetic Slices II - Subsequence | Hard | Solve |