Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1014. Best Sightseeing Pair #217

Open
Tcdian opened this issue Jun 17, 2020 · 1 comment
Open

1014. Best Sightseeing Pair #217

Tcdian opened this issue Jun 17, 2020 · 1 comment
Labels

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 17, 2020

1014. Best Sightseeing Pair

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

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

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

Example

Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11

Note

  • 2 <= A.length <= 50000
  • 1 <= A[i] <= 1000
@Tcdian Tcdian added the Array label Jun 17, 2020
@Tcdian
Copy link
Owner Author

Tcdian commented Jun 17, 2020

Solution

  • JavaScript Solution
/**
 * @param {number[]} A
 * @return {number}
 */
var maxScoreSightseeingPair = function(A) {
    let result = -Infinity;
    let maxScoreSightseeingPoint = A[0] + 0;
    for (let i = 1; i < A.length; i++) {
        result = Math.max(maxScoreSightseeingPoint + A[i] - i, result);
        maxScoreSightseeingPoint = Math.max(A[i] + i, maxScoreSightseeingPoint);
    }
    return result;
};
  • TypeScript Solution
function maxScoreSightseeingPair(A: number[]): number {
    let result = -Infinity;
    let maxScoreSightseeingPoint = A[0] + 0;
    for (let i = 1; i < A.length; i++) {
        result = Math.max(maxScoreSightseeingPoint + A[i] - i, result);
        maxScoreSightseeingPoint = Math.max(A[i] + i, maxScoreSightseeingPoint);
    }
    return result;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant