Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 2498. Frog Jump II #159

Open
Woodyiiiiiii opened this issue Dec 11, 2022 · 0 comments
Open

Leetcode 2498. Frog Jump II #159

Woodyiiiiiii opened this issue Dec 11, 2022 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

这道题是双周赛第三题。

一开始我比较懵,不知道如何利用条件。但慢慢通过举例子,观察用例,可以发现其中包含的模型和规律:为了最大化确保答案,我们只需要观察每个位置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;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant