diff --git a/leetcode/801-900/0813.Largest-Sum-of-Averages/README.md b/leetcode/801-900/0813.Largest-Sum-of-Averages/README.md index 724a2dc88..f3bfcafa4 100644 --- a/leetcode/801-900/0813.Largest-Sum-of-Averages/README.md +++ b/leetcode/801-900/0813.Largest-Sum-of-Averages/README.md @@ -1,28 +1,29 @@ # [813.Largest Sum of Averages][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an integer array `nums` and an integer `k`. You can partition the array into **at most** k non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray. + +Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer. + +Return the maximum **score** you can achieve of all the possible partitions. Answers within `10^-6` of the actual answer will be accepted. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [9,1,2,3,9], k = 3 +Output: 20.00000 +Explanation: +The best choice is to partition nums into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20. +We could have also partitioned nums into [9, 1], [2], [3, 9], for example. +That partition would lead to a score of 5 + 2 + 6 = 13, which is worse. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Largest Sum of Averages -```go ``` - +Input: nums = [1,2,3,4,5,6,7], k = 4 +Output: 20.50000 +``` ## 结语 diff --git a/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution.go b/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution.go index d115ccf5e..f4c37f845 100644 --- a/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution.go +++ b/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution.go @@ -1,5 +1,42 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) float64 { + l := len(nums) + cache := make([][]float64, l) + sum := 0 + mm := 0 + for i := 0; i < l; i++ { + sum += nums[i] + mm = max(mm, nums[i]) + cache[i] = make([]float64, k+1) + for j := 0; j <= k; j++ { + cache[i][j] = -1.0 + } + cache[i][1] = float64(sum) / float64(i+1) + if i+1 <= k { + cache[i][i+1] = float64(sum) + } + } + var dfs func(int, int) float64 + dfs = func(index, kk int) float64 { + if index < 0 { + return 0 + } + if r := cache[index][kk]; r+1.0 > 0.000001 { + return r + } + ans := float64(0) + cur := 0 + for c := index; c >= kk-1; c-- { + cur += nums[c] + avg := float64(cur) / float64(index-c+1) + + if r := dfs(c-1, kk-1); r+1.0 > 0.000001 { + ans = max(ans, avg+r) + } + } + cache[index][kk] = ans + return ans + } + return dfs(l-1, k) } diff --git a/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution_test.go b/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution_test.go index 14ff50eb4..4f70a4d63 100644 --- a/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution_test.go +++ b/leetcode/801-900/0813.Largest-Sum-of-Averages/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect float64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{9, 1, 2, 3, 9}, 3, 20.00000}, + {"TestCase2", []int{1, 2, 3, 4, 5, 6, 7}, 4, 20.50000}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }