Woodyiiiiiii / LeetCode

My private record of Leetcode solution

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leetcode 2498. Frog Jump II

Woodyiiiiiii opened this issue · comments

这道题是双周赛第三题。

一开始我比较懵,不知道如何利用条件。但慢慢通过举例子,观察用例,可以发现其中包含的模型和规律:为了最大化确保答案,我们只需要观察每个位置stone与前两个位置stone[i-2]相比即可。

class Solution {
    public int maxJump(int[] stones) {
        int n = stones.length;
        if (n == 2) {
            return Math.abs(stones[1] - stones[0]);
        } else if (n == 3) {
            return Math.abs(stones[2] - stones[0]);
        }

        int ans = 0;
        for (int i = 2; i < n; i++) {
            ans = Math.max(ans, stones[i] - stones[i - 2]);
        }

        return ans;
    }
}