avatar

Catalog
最佳观光组合

解题思路

主要要理解:最大化 A[i]+i+A[j]-j 的值其实就等价于求 [0,j-1] 中 A[i]+i 的最大值 mx,景点 j 的答案即为 mx+A[j]-j

代码

java
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int maxScoreSightseeingPair(int[] A) {
int res = 0;
int mx = A[0]+0;
for(int j=1;j<A.length;j++){
res = Math.max(res, mx+A[j]-j);
mx = Math.max(mx, A[j]+j);
}
return res;
}
}
Author: kim yhow
Link: http://yoursite.com/2020/06/17/最佳观光组合/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付寶
    支付寶