题目链接:https://leetcode.com/problems/best-sightseeing-pair/
解题思路:参考博客,想法非常简洁,即:step1是在两个可选的值中取最大值返回,step2找到已经遍历过的序列中最大值,但是掺杂距离,因此这个最大值会随着游标向右移而减小,以此作为它对应的新的最大值.
解题代码:
class Solution {
public:
int maxScoreSightseeingPair(vector<int>& A) {
int cur=0,res=0;
for(int i=0;i<A.size();i++){
res=max(res,cur+A[i]); // step1
cur=max(cur,A[i])-1; //step2
}
return res;
}
};