[Swift]LeetCode1014. 最佳觀光組合 | Best Sightseeing Pair

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-upilnvmd-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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.github

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:app

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

給定正整數數組 AA[i] 表示第 i 個觀光景點的評分,而且兩個景點 i 和 j 之間的距離爲 j - ispa

一對景點(i < j)組成的觀光組合的得分爲(A[i] + A[j] + i - j):景點的評分之和減去它們二者之間的距離。code

返回一對觀光景點能取得的最高分。htm

示例:blog

輸入:[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: 404 ms
Memory Usage: 19.7 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 }

404ms
 1 class Solution {
 2     func maxScoreSightseeingPair(_ A: [Int]) -> Int {
 3         var maxScore = Int.min
 4         var leftHighScoreIndex = 0
 5         var leftHighScoreValue = A[leftHighScoreIndex]
 6         for i in 1..<A.count {
 7             maxScore = max(leftHighScoreValue + A[i] + leftHighScoreIndex - i, maxScore)
 8             if (A[i] - leftHighScoreValue) + (i - leftHighScoreIndex) >= 0 {
 9                 leftHighScoreIndex = i 
10                 leftHighScoreValue = A[i]
11             }
12         }
13         return maxScore
14     }
15 }

440ms

 1 class Solution {
 2     func maxScoreSightseeingPair(_ A: [Int]) -> Int {
 3         // B: store max of A[0]+0 ~ A[i]+i
 4         var B = A, currMax = B[0] + 0
 5         for i in 0..<B.count {
 6             currMax = max(B[i] + i, currMax)
 7             B[i] = currMax
 8         }
 9         
10         var dp = A.map { _ in 0 }
11         for i in 1..<dp.count {
12             dp[i] = max(dp[i-1], A[i] - i + B[i-1])
13         }
14         
15         return dp.last!
16     }
17 }

460ms

 1 class Solution {
 2     func maxScoreSightseeingPair(_ A: [Int]) -> Int {
 3         var score = 0
 4         
 5         var s1 = Array<Int>()
 6         s1.reserveCapacity(A.count)
 7       
 8         for i in 0..<A.count {
 9             s1.append(A[i] + i)
10         }
11         
12         var m = Array<Int>()
13         m.append(A.last! - (A.count - 1))
14         
15         for i in (1..<A.count) {
16            m.append(max(A[A.count - i - 1] - (A.count - i - 1), m[i - 1]))
17         }
18         m.reverse()
19       
20         for i in 0..<(A.count-1) {
21             score = max(score, s1[i] + m[i + 1])
22         }
23         
24         
25         return score
26     }
27 }

464ms

 1 class Solution {
 2     func maxScoreSightseeingPair(_ A: [Int]) -> Int {
 3         var tmp = Int.min, result = Int.min
 4         for (offset, element) in A.enumerated() {
 5             result = max(result, element - offset + tmp)
 6             tmp = max(element + offset, tmp)
 7         }
 8         
 9         return result
10     }
11 }

524ms

 1 class Solution {
 2     func maxScoreSightseeingPair(_ A: [Int]) -> Int {
 3         if A.count < 2 { return -1 }        
 4         var cache = [Int]()        
 5         cache.append(0)        
 6         var res = Int.min        
 7         for idx in 1 ..< A.count {            
 8             res = max(res, A[cache.last!] + A[idx] + cache.last! - idx)
 9             if A[idx] - cache.last! + idx > A[cache.last!] {
10                 cache.append(idx)
11             }
12         }        
13         return res        
14     }
15 }
相關文章
相關標籤/搜索