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;
    }
}
posted on 2019-03-24 17:49  王 帅  阅读(78)  评论(0编辑  收藏  举报