Skip to content

Commit 8f89ee6

Browse files
committed
Add solution and test-cases for problem 1792
1 parent dbd4dda commit 8f89ee6

File tree

3 files changed

+81
-26
lines changed

3 files changed

+81
-26
lines changed

leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# [1792.Maximum Average Pass Ratio][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+
There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array `classes`, where `classes[i] = [passi, totali]`. You know beforehand that in the `ith` class, there are `totali` total students, but only `passi` number of students will pass the exam.
5+
6+
You are also given an integer `extraStudents`. There are another `extraStudent` brilliant students that are **guaranteed** to pass the exam of any class they are assigned to. You want to assign each of the `extraStudents` students to a class in a way that **maximizes** the **average** pass ratio across **all** the classes.
7+
8+
The **pass ratio** of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The **average pass ratio** is the sum of pass ratios of all the classes divided by the number of the classes.
9+
10+
Return the **maximum** possible average pass ratio after assigning the `extraStudents` students. Answers within `10^-5` of the actual answer will be accepted.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2
16+
Output: 0.78333
17+
Explanation: You can assign the two extra students to the first class. The average pass ratio will be equal to (3/4 + 3/5 + 2/2) / 3 = 0.78333.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Average Pass Ratio
23-
```go
2422
```
25-
23+
Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4
24+
Output: 0.53485
25+
```
2626

2727
## 结语
2828

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
type heap1792 [][]int
6+
7+
func (h *heap1792) Len() int {
8+
return len(*h)
9+
}
10+
11+
func (h *heap1792) Swap(i, j int) {
12+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
13+
}
14+
15+
func (h *heap1792) Less(i, j int) bool {
16+
a, b := (*h)[i], (*h)[j]
17+
// 判断增幅
18+
aa := (float64(a[0]+1) / float64(a[1]+1)) - (float64(a[0]) / float64(a[1]))
19+
bb := (float64(b[0]+1) / float64(b[1]+1)) - (float64(b[0]) / float64(b[1]))
20+
return aa > bb
21+
}
22+
23+
func (h *heap1792) Push(x any) {
24+
*h = append(*h, x.([]int))
25+
}
26+
27+
func (h *heap1792) Pop() any {
28+
old := *h
29+
l := len(old)
30+
x := old[l-1]
31+
*h = old[:l-1]
432
return x
533
}
34+
35+
func Solution(classes [][]int, extraStudents int) float64 {
36+
h := &heap1792{}
37+
var ans float64 = 0
38+
for _, c := range classes {
39+
if c[0] == c[1] {
40+
// 永远都是1提升不了
41+
ans++
42+
continue
43+
}
44+
heap.Push(h, c)
45+
}
46+
if h.Len() > 0 {
47+
for ; extraStudents > 0; extraStudents-- {
48+
top := heap.Pop(h).([]int)
49+
top[0]++
50+
top[1]++
51+
heap.Push(h, top)
52+
}
53+
for h.Len() > 0 {
54+
top := heap.Pop(h).([]int)
55+
ans += float64(top[0]) / float64(top[1])
56+
}
57+
}
58+
59+
return ans / float64(len(classes))
60+
}

leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
classes [][]int
14+
extraStudents int
15+
expect float64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{1, 2}, {3, 5}, {2, 2}}, 2, 0.7833333333333333},
18+
{"TestCase2", [][]int{{2, 4}, {3, 9}, {4, 5}, {2, 10}}, 4, 0.5348484848484849},
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.classes, c.extraStudents)
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.classes, c.extraStudents)
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)