题目: 1014. Best Sightseeing Pair
分析
这道题求 the maximum score of a pair of sightseeing spots; score = A[i] + A[j] + i - j)。 是个最优解问题。
迭代中,当前idx为i; 要考虑 (0, i-1)的数中对score增益最大的数的下标是max_idx。 计算 (max_idx, i)的score, 并跟之前最大值做比较。
1 | int maxScoreSightseeingPair(vector<int> & A) { |
更为简约的写法:
max_inc每次迭代后都自减1,是因为进入下一次迭代i+1的时候,它对分数的增益只剩下了A[max_inc] + i - (i+1)。
1 | int maxScoreSightseeingPair2(vector<int>& A) { |
时间复杂度: $o(n)$
scan qr code and share this article