Skip to content

Commit 2381c30

Browse files
committed
Add solution and test-cases for problem 813
1 parent 09a1dcb commit 2381c30

File tree

3 files changed

+64
-26
lines changed

3 files changed

+64
-26
lines changed

leetcode/801-900/0813.Largest-Sum-of-Averages/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [813.Largest Sum of Averages][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
7+
8+
Return the maximum **score** you can achieve of all the possible partitions. Answers within `10^-6` of the actual answer will be accepted.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [9,1,2,3,9], k = 3
14+
Output: 20.00000
15+
Explanation:
16+
The best choice is to partition nums into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
17+
We could have also partitioned nums into [9, 1], [2], [3, 9], for example.
18+
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Largest Sum of Averages
23-
```go
2423
```
25-
24+
Input: nums = [1,2,3,4,5,6,7], k = 4
25+
Output: 20.50000
26+
```
2627

2728
## 结语
2829

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) float64 {
4+
l := len(nums)
5+
cache := make([][]float64, l)
6+
sum := 0
7+
mm := 0
8+
for i := 0; i < l; i++ {
9+
sum += nums[i]
10+
mm = max(mm, nums[i])
11+
cache[i] = make([]float64, k+1)
12+
for j := 0; j <= k; j++ {
13+
cache[i][j] = -1.0
14+
}
15+
cache[i][1] = float64(sum) / float64(i+1)
16+
if i+1 <= k {
17+
cache[i][i+1] = float64(sum)
18+
}
19+
}
20+
var dfs func(int, int) float64
21+
dfs = func(index, kk int) float64 {
22+
if index < 0 {
23+
return 0
24+
}
25+
if r := cache[index][kk]; r+1.0 > 0.000001 {
26+
return r
27+
}
28+
ans := float64(0)
29+
cur := 0
30+
for c := index; c >= kk-1; c-- {
31+
cur += nums[c]
32+
avg := float64(cur) / float64(index-c+1)
33+
34+
if r := dfs(c-1, kk-1); r+1.0 > 0.000001 {
35+
ans = max(ans, avg+r)
36+
}
37+
}
38+
cache[index][kk] = ans
39+
return ans
40+
}
41+
return dfs(l-1, k)
542
}

leetcode/801-900/0813.Largest-Sum-of-Averages/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect float64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{9, 1, 2, 3, 9}, 3, 20.00000},
18+
{"TestCase2", []int{1, 2, 3, 4, 5, 6, 7}, 4, 20.50000},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)