1014. 最佳观光组合
https://leetcode-cn.com/problems/best-sightseeing-pair/
解法一:
求A[i]+A[j]+i-j
可以分解为A[i]+i
和A[j]-j
,由于i<j,于是一次遍历,用pre_max记录A[i]+i
最大值
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = 0
pre_max = 0
for j in range(len(A)):
res = max(res, A[j] - j + pre_max)
pre_max = max(pre_max, A[j]+j)
return res
最后更新于