为有牺牲多壮志,敢教日月换新天。

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10562722.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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?

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

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 数组中的每一个数进行分组,并且分数不一定需要是整数。

示例:
输入: 
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, 但不是最大值.

说明:

  • 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 }

92ms

 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 }

100ms

 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 }

188ms

 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 }

 

posted @ 2019-03-20 09:00  为敢技术  阅读(362)  评论(0编辑  收藏  举报