Skip to main content
Back to problems
#2498
Medium Algorithms

Frog jump ii

Array Binary Search Greedy
62.4% acceptance
Feb 25, 2026
870
127
You are given stones sorted in strictly increasing order. A frog starts at stones[0], travels to the last stone and back, each stone visited at most once. Return the minimum cost = minimum possible max jump length. Key insight: optimal strategy is to alternate stones between the two paths. So the answer is max of differences between alternate-indexed stones: max(stones[2]-stones[0], stones[3]-stones[1], stones[4]-stones[2], ...)

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn max_jump(stones: Vec<i32>) -> i32 {
    let n = stones.len();
    let mut ans = 0i32;
    for i in 2..n {
      ans = ans.max(stones[i] - stones[i - 2]);
    }
    // Edge: last jump on return path = stones[n-1] if n=2
    ans.max(if n >= 2 { stones[1] - stones[0] } else { 0 })
  }
}