[Swift]LeetCode813. 最大平均值和的分組 | Largest Sum of Averages

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

We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?git

Note that our partition must use every number in A, and that scores are not necessarily integers.github

Example:
Input: 
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation: 
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.

Note:數組

  • 1 <= A.length <= 100.
  • 1 <= A[i] <= 10000.
  • 1 <= K <= A.length.
  • Answers within 10^-6 of the correct answer will be accepted as correct.

咱們將給定的數組 A 分紅 K 個相鄰的非空子數組 ,咱們的分數由每一個子數組內的平均值的總和構成。計算咱們所能獲得的最大分數是多少。微信

注意咱們必須使用 A 數組中的每個數進行分組,而且分數不必定須要是整數。spa

示例:
輸入: 
A = [9,1,2,3,9]
K = 3
輸出: 20
解釋: 
A 的最優分組是[9], [1, 2, 3], [9]. 獲得的分數是 9 + (1 + 2 + 3) / 3 + 9 = 20.
咱們也能夠把 A 分紅[9, 1], [2], [3, 9].
這樣的分組獲得的分數爲 5 + 2 + 6 = 13, 但不是最大值.

說明:code

  • 1 <= A.length <= 100.
  • 1 <= A[i] <= 10000.
  • 1 <= K <= A.length.
  • 答案偏差在 10^-6 內被視爲是正確的。

Runtime: 64 ms
Memory Usage: 19.2 MB
 1 class Solution {
 2     func largestSumOfAverages(_ A: [Int], _ K: Int) -> Double {
 3         var n:Int = A.count
 4         var sums:[Double] = [Double](repeating:0,count:n + 1)
 5         var dp:[Double] = [Double](repeating:0,count:n + 1)
 6         for i in 0..<n
 7         {
 8             sums[i + 1] = sums[i] + Double(A[i])
 9         }
10         for i in 0..<n
11         {
12             dp[i] = (sums[n] - sums[i]) / Double(n - i)
13         }
14         for k in 1..<K
15         {
16             for i in 0..<(n - 1)
17             {
18                 for j in (i + 1)..<n
19                 {
20                     dp[i] = max(dp[i], (sums[j] - sums[i]) / Double(j - i) + dp[j])
21                 }
22             }
23         }
24         return dp[0]
25     }
26 }

92mshtm

 1 private struct State: Hashable {
 2     var i: Int
 3     var K: Int
 4 }
 5 
 6 class Solution {
 7     func largestSumOfAverages(_ A: [Int], _ K: Int) -> Double {
 8         var dict = [State: Double]()
 9         return largestSumOfAverages(A, K, 0, &dict)
10     }
11     
12     private func largestSumOfAverages(_ A: [Int], _ K: Int, _ i: Int, _ dict: inout [State: Double]) -> Double {
13         guard i < A.count else { return 0 }
14         if let val = dict[State(i: i, K: K)] {
15             return val
16         }
17         var sum: Double = 0.0
18         var largest: Double = 0.0
19         for j in i..<A.count {
20             sum += Double(A[j])
21             let avg = sum / Double(j - i + 1)
22             if K - 1 > 0 {
23                 largest = max(largest, avg + largestSumOfAverages(A, K - 1, j + 1, &dict))
24             }
25         }
26         largest = max(largest, sum / Double(A.count - i))
27         dict[State(i: i, K: K)] = largest
28         return largest
29     }
30 }

100msblog

 1 class Solution {    
 2     // memo[k][i] 前 i 個 元素分 k 份產生的最大值
 3     var memo: [[Double]]!
 4     var sum: [Double]!
 5     
 6     func largestSumOfAverages(_ A: [Int], _ K: Int) -> Double {
 7         let n = A.count
 8         if n == 0 {
 9             return 0
10         }
11         memo = Array(repeating: Array(repeating: 0.0, count: n+1), count: K+1)
12         sum = Array(repeating: 0, count: n+1)
13         for i in 1...n {
14             sum[i] = sum[i-1] + Double(A[i-1])
15         }
16         return LSA(A, n, K)
17     }
18     
19     func LSA(_ A: [Int], _ n: Int, _ k: Int) -> Double {
20         if memo[k][n] > 0 {
21             return memo[k][n]
22         }
23         if k == 1 {
24             return sum[n] / Double(n)
25         }
26         for i in k-1..<n {
27             // 老是要分紅最後一個和前面幾個的, 只是不知道那裏是最後一個, 有點像走一步兩步的那個思路
28             memo[k][n] = max(memo[k][n], LSA(A, i, k-1) + (sum[n] - sum[i]) / Double(n - i))
29         }
30         return memo[k][n]
31     }
32 }

188msget

 1 class Solution {
 2     func largestSumOfAverages(_ A: [Int], _ K: Int) -> Double {
 3         var cache = [State: Double]()
 4         return largestSum(A, K, 0, &cache)
 5     }
 6     
 7     func largestSum(_ A: [Int], _ K: Int, _ index: Int, _ cache: inout [State: Double]) -> Double {
 8         let count = A.count
 9         guard count > index else { return 0 }
10         var sum: Double = 0
11         for i in index..<count { sum += Double(A[i]) }
12             
13         guard K > 1 else {
14             return sum / Double(count - index)
15         }
16         let state = State(x: K, y: index)
17         if let val = cache[state] { return val }
18         var mx: Double = 0
19         sum = 0
20         var totalElements = 0
21         for i in index..<count {
22             totalElements += 1
23             sum += Double(A[i])
24             mx = max(mx, sum / Double(totalElements) + largestSum(A, K - 1, i + 1, &cache))
25         }
26         cache[state] = mx
27         return mx
28     }
29 }
30 
31 struct State: Hashable {
32     var x: Int
33     var y: Int
34 }
相關文章
相關標籤/搜索