[Swift Weekly Contest 129]LeetCode1021. 最佳觀光組合 | Best Sightseeing Pair
阿新 • • 發佈:2019-03-24
bsp 提示 var pre see values repr input example
)組成的觀光組合的得分為(
Runtime: 432 ms Memory Usage: 19.6 MB
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:
2 <= A.length <= 50000
1 <= A[i] <= 1000
給定正整數數組 A
,A[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
提示:
2 <= A.length <= 50000
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 }
[Swift Weekly Contest 129]LeetCode1021. 最佳觀光組合 | Best Sightseeing Pair