diff --git a/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/README.md b/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/README.md index a43abd617..df92bacd1 100755 --- a/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/README.md +++ b/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution.go b/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution.go index d115ccf5e..829835ffe 100644 --- a/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution.go +++ b/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution.go @@ -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)) +} diff --git a/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution_test.go b/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution_test.go index 14ff50eb4..c46dade19 100644 --- a/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution_test.go +++ b/leetcode/1701-1800/1792.Maximum-Average-Pass-Ratio/Solution_test.go @@ -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() { }