You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
}
这道题是双周赛第三题。
一开始我比较懵,不知道如何利用条件。但慢慢通过举例子,观察用例,可以发现其中包含的模型和规律:为了最大化确保答案,我们只需要观察每个位置stone与前两个位置stone[i-2]相比即可。
The text was updated successfully, but these errors were encountered: