leetcode 1021. Best Sightseeing Pair
For each position, use A[i] to record the max value of A[j] + j, which j <= i.
And iterate the array, in each round, we can compute the maximum value where i picked from the previous and j fixed to the current index.
class Solution {
public int maxScoreSightseeingPair(int[] A) {
int ret = 0;
for (int i = 1; i < A.length; ++i) {
ret = Math.max(A[i - 1] + A[i] - i, ret);
A[i] = Math.max(A[i - 1], A[i] + i);
}
return ret;
}
}