Skip to content

Commit

Permalink
Add solution and test-cases for problem 813
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed May 1, 2024
1 parent 09a1dcb commit 2381c30
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
29 changes: 15 additions & 14 deletions leetcode/801-900/0813.Largest-Sum-of-Averages/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
41 changes: 39 additions & 2 deletions leetcode/801-900/0813.Largest-Sum-of-Averages/Solution.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 10 additions & 10 deletions leetcode/801-900/0813.Largest-Sum-of-Averages/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

0 comments on commit 2381c30

Please sign in to comment.