其他分享
首页 > 其他分享> > [Swift Weekly Contest 129]LeetCode1021. 最佳观光组合 | Best Sightseeing Pair

[Swift Weekly Contest 129]LeetCode1021. 最佳观光组合 | Best Sightseeing Pair

作者:互联网

Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.

The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.

Return the maximum score of a pair of sightseeing spots.

Example 1:

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:

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

给定正整数数组 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

Runtime: 432 ms Memory Usage: 19.6 MB
 1 class Solution {
 2     func maxScoreSightseeingPair(_ A: [Int]) -> Int {
 3         var n:Int = A.count
 4         var best:Int = -Int.max
 5         var most:Int = -Int.max
 6         for i in 0..<n
 7         {
 8             best = max(best, A[i] - i + most)
 9             most = max(most, A[i] + i)
10         }
11         return best       
12     }
13 }

 

标签:11,Contest,Int,LeetCode1021,观光,spots,景点,Pair,sightseeing
来源: https://www.cnblogs.com/strengthen/p/10587905.html