Frog Jump II - Problem

You are given a 0-indexed integer array stones sorted in strictly increasing order representing the positions of stones in a river.

A frog, initially on the first stone, wants to travel to the last stone and then return to the first stone. However, it can jump to any stone at most once.

The length of a jump is the absolute difference between the position of the stone the frog is currently on and the position of the stone to which the frog jumps.

More formally, if the frog is at stones[i] and is jumping to stones[j], the length of the jump is |stones[i] - stones[j]|.

The cost of a path is the maximum length of a jump among all jumps in the path.

Return the minimum cost of a path for the frog.

Input & Output

Example 1 — Basic Case
$ Input: stones = [0,2,5,6,7]
Output: 3
💡 Note: One optimal path: 0→2→6→7→5→0 with jumps [2,4,1,2,5]. Maximum jump is 5, but optimal path gives 3.
Example 2 — Minimum Size
$ Input: stones = [0,10]
Output: 0
💡 Note: Only two stones, frog starts at first and needs to reach second then return. No intermediate jumps needed.
Example 3 — Three Stones
$ Input: stones = [0,3,9]
Output: 3
💡 Note: Path: 0→3→9→0. Jumps are [3,6,9]. Minimum maximum jump is 9, but with alternating strategy it's 3.

Constraints

  • 2 ≤ stones.length ≤ 105
  • 0 ≤ stones[i] ≤ 109
  • stones[0] = 0
  • stones is sorted in strictly increasing order

Visualization

Tap to expand
Frog Jump II: Round Trip with Minimum Cost🐸2567StartEndForward Path (Blue): Visit some stones to reach the endReturn Path (Orange): Visit remaining stones to return homeGoal: Minimize the maximum jump distanceConstraint: Each stone visited exactly once
Understanding the Visualization
1
Input
Stones positioned at [0,2,5,6,7] in a river
2
Round Trip
Frog goes 0→last→0, visiting each stone once
3
Minimize Cost
Find path with minimum maximum jump distance
Key Takeaway
🎯 Key Insight: The alternating strategy (even/odd positions) naturally minimizes maximum jump distances
Asked in
Google 15 Meta 12
12.8K Views
Medium Frequency
~25 min Avg. Time
456 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