2498. Frog Jump II LeetCode Solution

In this guide, you will get 2498. Frog Jump II LeetCode Solution with the best time and space complexity. The solution to Frog Jump II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Frog Jump II solution in C++
  4. Frog Jump II solution in Java
  5. Frog Jump II solution in Python
  6. Additional Resources
2498. Frog Jump II LeetCode Solution image

Problem Statement of Frog Jump II

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2498. Frog Jump II LeetCode Solution in C++

class Solution {
 public:
  int maxJump(vector<int>& stones) {
    // Let's denote the forwarding path as F and the backwarding path as B.
    // "F1 B2 B1 F2" is no better than "F1 B2 F2 B1" since the distance between
    // F1 and F2 increase, resulting a larger `ans`.
    int ans = stones[1] - stones[0];  // If there're only two stones.
    for (int i = 2; i < stones.size(); ++i)
      ans = max(ans, stones[i] - stones[i - 2]);
    return ans;
  }
};
/* code provided by PROGIEZ */

2498. Frog Jump II LeetCode Solution in Java

class Solution {
  public int maxJump(int[] stones) {
    // Let's denote the forwarding path as F and the backwarding path as B.
    // "F1 B2 B1 F2" is no better than "F1 B2 F2 B1" since the distance between
    // F1 and F2 increase, resulting a larger `ans`.
    int ans = stones[1] - stones[0]; // If there're only two stones.
    for (int i = 2; i < stones.length; ++i)
      ans = Math.max(ans, stones[i] - stones[i - 2]);
    return ans;
  }
}
// code provided by PROGIEZ

2498. Frog Jump II LeetCode Solution in Python

class Solution:
  def maxJump(self, stones: list[int]) -> int:
    # Let's denote the forwarding path as F and the backwarding path as B.
    # 'F1 B2 B1 F2' is no better than 'F1 B2 F2 B1' since the distance between
    # F1 and F2 increase, resulting a larger `ans`.
    if len(stones) == 2:
      return stones[1] - stones[0]
    return max(stones[i] - stones[i - 2]
               for i in range(2, len(stones)))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.