Frog Jump - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: stones = [0,1,3,5,6,8,12,17]
Output: true
💡 Note: Frog can jump: 0→1 (jump 1), 1→3 (jump 2), 3→5 (jump 2), 5→6 (jump 1), 6→8 (jump 2), 8→12 (jump 4), 12→17 (jump 5)
Example 2 — Impossible Case
$ Input: stones = [0,1,2,3,4,8,9,11]
Output: false
💡 Note: Gap from stone 4 to stone 8 is too large - frog cannot jump 4 units as maximum jump from position 4 would be 3 units
Example 3 — Edge Case
$ Input: stones = [0,1,3,6,10,15]
Output: true
💡 Note: Path exists: 0→1 (jump 1), 1→3 (jump 2), 3→6 (jump 3), 6→10 (jump 4), 10→15 (jump 5)

Constraints

  • 2 ≤ stones.length ≤ 2000
  • 0 ≤ stones[i] ≤ 231 - 1
  • stones[0] == 0
  • stones is sorted in strictly increasing order

Visualization

Tap to expand
Frog Jump: Cross River Using Stone Positions [0,1,3,5,6,8,12,17]River0135681217jump=1jump=2jump=2jump=1STARTTARGETJump Constraint: If last jump was k, next must be k-1, k, or k+1Valid Path Found: 0→1→3→5→6→8→12→17 ✓
Understanding the Visualization
1
Input
Array of stone positions: [0,1,3,5,6,8,12,17]
2
Process
Find valid path with jump constraints: k-1, k, k+1
3
Output
Return true if frog can reach the last stone
Key Takeaway
🎯 Key Insight: Track which jump sizes can reach each stone position using dynamic programming
Asked in
Google 45 Amazon 32 Facebook 28 Microsoft 25
67.0K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen