Frog Jump - Problem

A determined frog embarks on an epic river crossing adventure! 🐸

The river is divided into discrete units, and at various positions there are stones that our brave frog can land on. The frog starts on the first stone and must reach the last stone to complete the journey.

Here's the catch - the frog has momentum-based jumping rules:

  • The first jump must be exactly 1 unit
  • If the last jump was k units, the next jump can only be k-1, k, or k+1 units
  • The frog can only jump forward and must land on stones (never in water!)

Goal: Given a sorted array of stone positions, determine if the frog can successfully cross the river by reaching the last stone.

Input: An array stones where stones[i] represents the position of the i-th stone
Output: true if the frog can cross, false otherwise

Input & Output

example_1.py β€” Basic Success Case
$ Input: stones = [0,1,3,5,6,8,12,17]
β€Ί Output: true
πŸ’‘ Note: The frog can successfully cross: 0β†’1 (jump 1) β†’ 3 (jump 2) β†’ 5 (jump 2) β†’ 6 (jump 1) β†’ 8 (jump 2) β†’ 12 (jump 4) β†’ 17 (jump 5). Each jump follows the k-1, k, k+1 rule.
example_2.py β€” Impossible Case
$ Input: stones = [0,1,2,3,4,8,9,11]
β€Ί Output: false
πŸ’‘ Note: The frog cannot reach the end. From stone 4, the frog needs jump sizes 4, 5, or 6 to continue, but no stones exist at positions 8, 9, or 10 that would allow reaching stone 8.
example_3.py β€” Edge Case
$ Input: stones = [0,1,3,6,10,15,16,21]
β€Ί Output: true
πŸ’‘ Note: Path: 0β†’1 (jump 1) β†’ 3 (jump 2) β†’ 6 (jump 3) β†’ 10 (jump 4) β†’ 15 (jump 5) β†’ 16 (jump 1) β†’ 21 (jump 5). The frog can adjust jump size as needed.

Constraints

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

Visualization

Tap to expand
Frog Jump: Strategic River Crossing01356Goalk=1k=2k=2k=1DP State: (position, jump_size) β†’ possible?(0,1)β†’true(1,1)β†’true(3,2)β†’true(5,2)β†’true(6,1)β†’trueGoalβ†’βœ…
Understanding the Visualization
1
Initial Position
Frog starts at stone 0, must make first jump of exactly 1 unit
2
Momentum Rules
After jump of size k, next jump must be k-1, k, or k+1 units
3
Path Finding
Use DP to explore all valid combinations of (position, jump_size)
4
Success Check
Return true if any path reaches the final stone
Key Takeaway
🎯 Key Insight: By memoizing (position, jump_size) states, we avoid exponential recalculation and solve the problem efficiently in O(n²) time.
Asked in
Google 85 Amazon 72 Meta 58 Microsoft 45 Apple 31
89.7K Views
High Frequency
~25 min Avg. Time
2.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