Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1792 #1064

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# [1792.Maximum Average Pass Ratio][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
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.

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.

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.

Return the **maximum** possible average pass ratio after assigning the `extraStudents` students. Answers within `10^-5` of the actual answer will be accepted.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2
Output: 0.78333
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.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Maximum Average Pass Ratio
```go
```

Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4
Output: 0.53485
```

## 结语

Expand Down
57 changes: 56 additions & 1 deletion leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
package Solution

func Solution(x bool) bool {
import "container/heap"

type heap1792 [][]int

func (h *heap1792) Len() int {
return len(*h)
}

func (h *heap1792) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}

func (h *heap1792) Less(i, j int) bool {
a, b := (*h)[i], (*h)[j]
// 判断增幅
aa := (float64(a[0]+1) / float64(a[1]+1)) - (float64(a[0]) / float64(a[1]))
bb := (float64(b[0]+1) / float64(b[1]+1)) - (float64(b[0]) / float64(b[1]))
return aa > bb
}

func (h *heap1792) Push(x any) {
*h = append(*h, x.([]int))
}

func (h *heap1792) Pop() any {
old := *h
l := len(old)
x := old[l-1]
*h = old[:l-1]
return x
}

func Solution(classes [][]int, extraStudents int) float64 {
h := &heap1792{}
var ans float64 = 0
for _, c := range classes {
if c[0] == c[1] {
// 永远都是1提升不了
ans++
continue
}
heap.Push(h, c)
}
if h.Len() > 0 {
for ; extraStudents > 0; extraStudents-- {
top := heap.Pop(h).([]int)
top[0]++
top[1]++
heap.Push(h, top)
}
for h.Len() > 0 {
top := heap.Pop(h).([]int)
ans += float64(top[0]) / float64(top[1])
}
}

return ans / float64(len(classes))
}
22 changes: 11 additions & 11 deletions leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
classes [][]int
extraStudents int
expect float64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 2}, {3, 5}, {2, 2}}, 2, 0.7833333333333333},
{"TestCase2", [][]int{{2, 4}, {3, 9}, {4, 5}, {2, 10}}, 4, 0.5348484848484849},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.classes, c.extraStudents)
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.classes, c.extraStudents)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading