# 2498. Frog Jump II ###### tags: `Leetcode` `Medium` `Greedy` Link: https://leetcode.com/problems/frog-jump-ii/description/ ## 思路 思路和[这篇](https://leetcode.com/problems/frog-jump-ii/solutions/2897948/java-c-python-max-a-i-a-i-2/)写的一样 由于青蛙先从起点跳到终点 再跳回来 并且路上没有一个点用过两次 所以可以想象成两只青蛙用不同的路径分别从起点往终点跳 那么如何保证每条路径的每两点absolute difference最小的 就是间隔跳 assign stones to two frogs alternatively: ``` A[0] A[1] A[2] A[3] ... start frog1 frog2 frog1 ... ``` If we assign them not alternatively like: ```frog1 frog2 frog2 frog1``` The distance for frog 1 is huge and it's no better than ```frog1 frog2 frog1 frog2``` ## Code ```java= class Solution { public int maxJump(int[] stones) { int max = 0; int n = stones.length; int prev1 = stones[0], prev2 = stones[0]; for(int i=1; i<n-1; i++){ if(i%2==0){ max = Math.max(max, stones[i]-prev1); prev1 = stones[i]; } else{ max = Math.max(max, stones[i]-prev2); prev2 = stones[i]; } } max = Math.max(max, Math.max(stones[n-1]-prev1, stones[n-1]-prev2)); return max; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up