Skip to content

Latest commit

 

History

History
63 lines (43 loc) · 1.83 KB

1014-best-sightseeing-pair.md

File metadata and controls

63 lines (43 loc) · 1.83 KB

1014. Best Sightseeing Pair - 最佳观光组合

给定正整数数组 AA[i] 表示第 i 个观光景点的评分,并且两个景点 i 和 j 之间的距离为 j - i

一对景点(i < j)组成的观光组合的得分为(A[i] + A[j] + i - j):景点的评分之和减去它们两者之间的距离。

返回一对观光景点能取得的最高分。

 

示例:

输入:[8,1,5,2,6]
输出:11
解释:i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11

 

提示:

  1. 2 <= A.length <= 50000
  2. 1 <= A[i] <= 1000

题目标签:Array

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
java 4 ms 48.1 MB
class Solution {
    public int maxScoreSightseeingPair(int[] A) {
        if (A.length == 0) return 0;
        int[] B = new int[A.length];
        int[] C = new int[A.length];
        C[A.length - 1] = A[A.length - 1] - (A.length - 1);
        for (int i = A.length - 2; i >= 0; i--) {
            B[i] = A[i] + i;
            C[i] = Math.max(C[i + 1], A[i] - i);
        }
        int res = 0;
        for (int i = 0; i < A.length - 1; i++) {
            res = Math.max(res, B[i] + C[i + 1]);
        }
        return res;
    }
}