Leetcode 1014. Best Sightseeing Pair

本题是leetcode121这道题的翻版,做法完全一样,也是扫一遍数组,维护两个值,一个是a[i]+i的最大值,另一个是a[i]+a[j]+i-j的最大值.

class Solution:
    def maxScoreSightseeingPair(self, A: List[int]) -> int:
        best, ret = 0, 0
        for i, v in enumerate(A):
            ret = max(ret, best + v - i)
            best = max(best, i + v)
        return ret

 

posted @ 2019-04-12 22:16  周洋  阅读(485)  评论(0编辑  收藏  举报