Talentd
FleetCode
Make and follow sheets.Sign in

Frog Jump II solution and explanation

MediumArrayBinary SearchGreedy5 min read62.6% acceptanceUpdated 9 Apr 2026
Frog Jump II - Greedy - Leetcode 2498 - PythonCheatCode Ninja · 7:58 · 6K viewsWatch on YouTube
8 more videos on this problem
  1. Frog Jump II - Greedy - Leetcode 2498 - PythonCheatCode Ninja · 7:58 · 6K views
  2. Biweekly Contest 93 | Frog Jump IIcodingMohan · 20:30 · 3K views
  3. Leetcode 6263 || Frog Jump II || Leetcode Contest Medium || cppCoding Techniques with Komal | Newton School · 9:26 · 3K views
  4. 2498. Frog Jump II (Leetcode Medium)Programming Live with Larry · 16:03 · 2K views
  5. Leetcode | biweekly-contest-93 | 2498 Frog Jump IICyCode · 14:49 · 1K views
  6. 【每日一题】LeetCode 2498. Frog Jump IIHuifeng Guan · 12:21 · 557 views
  7. Leetcode 2498. Frog Jump II (greedy)LetsCode · 10:26 · 267 views
  8. Frog Jump II | DP Approach | Crack LeetCode 2498 with Optimal Strategy 💡Nayan Mandal · 22:54 · 82 views
  9. Dp-3 | Frog Jump with K Distance | Frog Jump II | DP Approach + LeetCode 2498 ExplainedNayan Mandal · 23:31 · 45 views

The 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.

Example 1:

Input: stones = [0,2,5,6,7]
Output: 5
Explanation: The above figure represents one of the optimal paths the frog can take.
The cost of this path is 5, which is the maximum length of a jump.
Since it is not possible to achieve a cost of less than 5, we return it.

Example 2:

Input: stones = [0,3,9]
Output: 9
Explanation: 
The frog can jump directly to the last stone and come back to the first stone. 
In this case, the length of each jump will be 9. The cost for the path will be max(9, 9) = 9.
It can be shown that this is the minimum achievable cost.

Constraints:

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

Approach overview

Problem Overview: You are given a sorted array of stone positions in a river. A frog must jump from the first stone to the last and then return to the start, visiting each stone at most once. The goal is to minimize the maximum jump distance made during the entire trip. The challenge is arranging the visiting order so the largest jump stays as small as possible.

Approach 1: Skipping Strategy using Two Segments (Greedy) (Time: O(n), Space: O(1))

The key observation is that visiting stones strictly in order creates large jumps on the return path. A better strategy splits stones into two alternating paths. The frog effectively jumps every other stone while going forward and uses the skipped stones on the way back. This pattern minimizes the largest gap encountered. Implementation becomes simple: iterate through the array and compute stones[i] - stones[i-2] for all i ≥ 2, tracking the maximum value. That value represents the worst jump the frog must make when using the optimal zigzag traversal. This works because alternating stones distributes the gaps more evenly across the route. The algorithm only scans the array once and uses constant extra memory, making it the optimal solution for this greedy pattern over a sorted array.

Approach 2: Dynamic Programming Approach (Time: O(n²), Space: O(n²))

A more explicit strategy models the order of visits using dynamic programming. Define states that track the minimum possible maximum jump after choosing certain stones for the forward path and leaving others for the return path. For each pair of stones, evaluate transitions that simulate choosing the next stone while updating the current worst jump distance. This approach systematically checks different distributions of stones between forward and backward paths. Although correct, the number of state transitions grows quickly, resulting in quadratic time and space complexity. This method helps understand the structure of the problem but is less practical for large inputs.

Some developers also reason about the answer using binary search on the maximum allowed jump and verifying feasibility. However, the greedy skipping insight eliminates the need for searching because the optimal pattern can be computed directly.

Recommended for interviews: The greedy skipping strategy is the expected solution. Interviewers want to see the insight that alternating stones minimizes the largest gap. A dynamic programming explanation shows deeper reasoning about path construction, but the O(n) greedy scan demonstrates strong pattern recognition and leads to the cleanest implementation.

Approach 1: Skipping Strategy using Two Segments

This approach focuses on dividing the journey into two segments. The frog can travel a path by skipping certain stones while minimizing the maximum jump in both the forward and return trips. The frog can either move in order and then skip back for the shortest jump, skipping intermediate stones optimally, or follow an alternate path with strategic skips across stones.

def min_jump_cost(stones):
    n = len(stones)
    if n == 2:
        return stones[1]
    max_jump = max(stones[i + 1] - stones[i - 1] for i in range(1, n - 1))
    return max(max_jump, stones[-1] - stones[-2], stones[1] - stones[0])

stones = [0, 2, 5, 6, 7]
print(min_jump_cost(stones))
import java.util.List;

public class FrogJump {
    public static int minJumpCost(List<Integer> stones) {
        int n = stones.size();
        if (n == 2) return stones.get(1);
        int maxJump = 0;
        for (int i = 1; i < n - 1; ++i) {
            maxJump = Math.max(maxJump, stones.get(i + 1) - stones.get(i - 1));
        }
        return Math.max(Math.max(maxJump, stones.get(n - 1) - stones.get(n - 2)), stones.get(1));
    }

    public static void main(String[] args) {
        List<Integer> stones = List.of(0, 2, 5, 6, 7);
        System.out.println(minJumpCost(stones));
    }
}
#include <vector>
#include <algorithm>
#include <iostream>

int min_jump_cost(std::vector<int>& stones) {
    int n = stones.size();
    if (n == 2) return stones[1];
    int max_jump = 0;
    for (int i = 1; i < n - 1; ++i) {
        max_jump = std::max(max_jump, stones[i + 1] - stones[i - 1]);
    }
    return std::max({max_jump, stones[n-1] - stones[n-2], stones[1]});
}

int main() {
    std::vector<int> stones = {0, 2, 5, 6, 7};
    std::cout << min_jump_cost(stones) << std::endl;
    return 0;
}
function minJumpCost(stones) {
    const n = stones.length;
    if (n === 2) return stones[1];
    let maxJump = 0;
    for (let i = 1; i < n - 1; ++i) {
        maxJump = Math.max(maxJump, stones[i + 1] - stones[i - 1]);
    }
    return Math.max(maxJump, stones[n - 1] - stones[n - 2], stones[1]);
}

const stones = [0, 2, 5, 6, 7];
console.log(minJumpCost(stones));
using System;
using System.Collections.Generic;

public class FrogJump {
    public static int MinJumpCost(List<int> stones) {
        int n = stones.Count;
        if (n == 2) return stones[1];
        int maxJump = 0;
        for (int i = 1; i < n - 1; ++i) {
            maxJump = Math.Max(maxJump, stones[i + 1] - stones[i - 1]);
        }
        return Math.Max(Math.Max(maxJump, stones[n - 1] - stones[n - 2]), stones[1]);
    }

    public static void Main() {
        List<int> stones = new List<int> {0, 2, 5, 6, 7};
        Console.WriteLine(MinJumpCost(stones));
    }
}

The Python function min_jump_cost calculates the minimum cost by searching for the maximum gap when skipping an intermediate stone. We calculate the largest possible jump needed when skipping each intermediate stone, including the direct start-to-end connection and the smallest immediate connections on both ends.

Complexity

Time Complexity: O(n), where n is the number of stones. The algorithm involves checking between all adjacent stones once.
Space Complexity: O(1), as it requires only constant extra space outside the input.

Try this approach in the editor

Approach 2: Dynamic Programming Approach

This approach uses dynamic programming to determine the minimal jump cost by considering all possible paths between stones. This involves using a memoization table to store and reuse calculations from previous steps.

The conceptual basis is to minimize the largest jump in any full round-trip path, adjusting calculations dynamically with each potential path resolution.

def min_jump_cost_dp(stones):
    n = len(stones)
    if n == 2:
        return stones[1]

    dp = [float('inf')] * n
    dp[0] = 0  # Start point

    for i in range(1, n):
        for j in range(i):
            dp[i] = min(dp[i], max(dp[j], stones[i] - stones[j]))
    return dp[n-1]

stones = [0, 2, 5, 6, 7]
print(min_jump_cost_dp(stones))
import java.util.List;
import java.util.Arrays;

public class FrogJumpDP {
    public static int minJumpCostDP(List<Integer> stones) {
        int n = stones.size();
        if (n == 2) return stones.get(1);

        int[] dp = new int[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;

        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                dp[i] = Math.min(dp[i], Math.max(dp[j], stones.get(i) - stones.get(j)));
            }
        }
        return dp[n - 1];
    }

    public static void main(String[] args) {
        List<Integer> stones = Arrays.asList(0, 2, 5, 6, 7);
        System.out.println(minJumpCostDP(stones));
    }
}
#include <vector>
#include <iostream>
#include <algorithm>

int min_jump_cost_dp(const std::vector<int>& stones) {
    int n = stones.size();
    if (n == 2) return stones[1];
    std::vector<int> dp(n, std::numeric_limits<int>::max());
    dp[0] = 0;

    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            dp[i] = std::min(dp[i], std::max(dp[j], stones[i] - stones[j]));
        }
    }
    return dp[n-1];
}

int main() {
    std::vector<int> stones = {0, 2, 5, 6, 7};
    std::cout << min_jump_cost_dp(stones) << std::endl;
    return 0;
}
function minJumpCostDP(stones) {
    const n = stones.length;
    if (n === 2) return stones[1];

    const dp = Array(n).fill(Infinity);
    dp[0] = 0;

    for (let i = 1; i < n; i++) {
        for (let j = 0; j < i; j++) {
            dp[i] = Math.min(dp[i], Math.max(dp[j], stones[i] - stones[j]));
        }
    }
    return dp[n - 1];
}

const stones = [0, 2, 5, 6, 7];
console.log(minJumpCostDP(stones));
using System;
using System.Collections.Generic;

public class FrogJumpDP {
    public static int MinJumpCostDP(List<int> stones) {
        int n = stones.Count;
        if (n == 2) return stones[1];

        int[] dp = new int[n];
        for (int i = 0; i < n; i++) dp[i] = int.MaxValue;
        dp[0] = 0;

        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                dp[i] = Math.Min(dp[i], Math.Max(dp[j], stones[i] - stones[j]));
            }
        }
        return dp[n - 1];
    }

    public static void Main() {
        List<int> stones = new List<int> {0, 2, 5, 6, 7};
        Console.WriteLine(MinJumpCostDP(stones));
    }
}

This dynamic programming solution in Python iteratively calculates minimal jump costs for each stone, adjusting based upon previous incremental costs while optimizing each step towards minimizing the maximal jump.

Complexity

Time Complexity: O(n2) due to nested loops for path calculations.
Space Complexity: O(n) for storage of interim results.

Try this approach in the editor

Approach 3: Default Approach

func maxJump(stones []int) int {
	ans := stones[1] - stones[0]
	for i := 2; i < len(stones); i++ {
		ans = max(ans, stones[i]-stones[i-2])
	}
	return ans
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Skipping Strategy using Two Segments

Time Complexity: O(n), where n is the number of stones. The algorithm involves checking between all adjacent stones once.
Space Complexity: O(1), as it requires only constant extra space outside the input.

Dynamic Programming Approach

Time Complexity: O(n2) due to nested loops for path calculations.
Space Complexity: O(n) for storage of interim results.

Default Approach–

Complexity analysis

ApproachTimeSpaceWhen to Use
Skipping Strategy using Two Segments (Greedy)O(n)O(1)Best choice for interviews and production. Uses the alternating stone insight to directly compute the minimal maximum jump.
Dynamic ProgrammingO(n²)O(n²)Useful for reasoning about different visit orders or when exploring state transitions during problem analysis.

Frequently asked questions

Is Frog Jump II easy or hard?

Frog Jump II is considered a Medium difficulty problem. The implementation is straightforward once you recognize the alternating stone pattern, but identifying the greedy insight that minimizes the maximum jump can take some reasoning.

Frog Jump II Python/Java solution

In both Python and Java, the solution iterates through the array starting from index 2 and computes stones[i] - stones[i-2]. Maintain a variable tracking the maximum difference encountered. The final value is the minimal possible maximum jump distance for the round trip.

How to solve Frog Jump II in O(n)?

Iterate through the stone positions and compute the distance between stones two steps apart: stones[i] - stones[i-2]. Track the maximum of these distances across the array. This represents the largest jump when stones are visited in an alternating pattern, which minimizes the maximum jump for the full forward and return trip.

What is the best approach for Frog Jump II?

The best approach is a greedy skipping strategy. Instead of visiting stones strictly in order, the frog alternates stones so the forward path uses every other stone and the remaining stones are used on the return path. This reduces the largest jump and leads to a simple O(n) scan computing the maximum value of stones[i] - stones[i-2].

Is Frog Jump II asked at Google/Amazon/Meta?

Greedy array optimization problems similar to Frog Jump II frequently appear in interviews at companies like Amazon, Google, and Meta. While the exact problem may vary, recognizing patterns that minimize maximum gaps using greedy strategies is a common interview theme.

What data structure is used in Frog Jump II?

The problem primarily uses a sorted array of stone positions. The optimal solution relies on greedy reasoning and simple iteration over the array rather than complex data structures.

What is the time complexity of Frog Jump II?

The optimal greedy solution runs in O(n) time and O(1) space because it scans the sorted stone array once and tracks the maximum gap between stones two indices apart. A dynamic programming formulation exists but typically requires O(n²) time and space.

Comments

Now write it yourselfClose this page and solve Frog Jump II in the editor, without looking.
Practise Frog Jump II