Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
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 <= 1050 <= stones[i] <= 109stones[0] == 0stones is sorted in a strictly increasing order.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Greedy
[0,2,5,6,7]
[0,3,9]
climbing-stairs)koko-eating-bananas)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2498: Frog Jump II
class Solution {
public int maxJump(int[] stones) {
int ans = stones[1] - stones[0];
for (int i = 2; i < stones.length; ++i) {
ans = Math.max(ans, stones[i] - stones[i - 2]);
}
return ans;
}
}
// Accepted solution for LeetCode #2498: Frog Jump II
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
}
# Accepted solution for LeetCode #2498: Frog Jump II
class Solution:
def maxJump(self, stones: List[int]) -> int:
ans = stones[1] - stones[0]
for i in range(2, len(stones)):
ans = max(ans, stones[i] - stones[i - 2])
return ans
// Accepted solution for LeetCode #2498: Frog Jump II
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2498: Frog Jump II
// class Solution {
// public int maxJump(int[] stones) {
// int ans = stones[1] - stones[0];
// for (int i = 2; i < stones.length; ++i) {
// ans = Math.max(ans, stones[i] - stones[i - 2]);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2498: Frog Jump II
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2498: Frog Jump II
// class Solution {
// public int maxJump(int[] stones) {
// int ans = stones[1] - stones[0];
// for (int i = 2; i < stones.length; ++i) {
// ans = Math.max(ans, stones[i] - stones[i - 2]);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.