返回介绍

solution / 2400-2499 / 2498.Frog Jump II / README_EN

发布于 2024-06-17 01:03:04 字数 3732 浏览 0 评论 0 收藏 0

2498. Frog Jump II

中文文档

Description

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.

Solutions

Solution 1

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
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;
  }
}
class Solution {
public:
  int maxJump(vector<int>& stones) {
    int ans = stones[1] - stones[0];
    for (int i = 2; i < stones.size(); ++i) ans = max(ans, stones[i] - stones[i - 2]);
    return ans;
  }
};
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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文