|
46 | 46 |
|
47 | 47 | <!-- 这里可写通用的实现逻辑 -->
|
48 | 48 |
|
| 49 | +**方法一:优先队列(增量大根堆)** |
| 50 | + |
| 51 | +假设一个班级当前的通过率为 $\frac{a}{b}$,那么如果我们将一个聪明的学生安排到此班级,那么班级的通过率就会变为 $\frac{a+1}{b+1}$。我们可以发现,通过率的增量为 $\frac{a+1}{b+1} - \frac{a}{b}$。 |
| 52 | + |
| 53 | +我们维护一个大根堆,堆中存储的是每个班级的通过率增量。 |
| 54 | + |
| 55 | +进行 `extraStudents` 次操作,每次从堆顶取出一个班级,将这个班级的人数和通过人数都加 $1$,然后将这个班级的通过率增量重新计算并放回堆中。重复这个过程,直到将所有的学生都分配完毕。 |
| 56 | + |
| 57 | +最后,我们将所有班级的通过率求和,然后除以班级数目,即为答案。 |
| 58 | + |
| 59 | +时间复杂度 $O(n\log n)$,其中 $n$ 为班级数目。 |
| 60 | + |
49 | 61 | <!-- tabs:start -->
|
50 | 62 |
|
51 | 63 | ### **Python3**
|
52 | 64 |
|
53 | 65 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 66 |
|
55 | 67 | ```python
|
56 |
| - |
| 68 | +class Solution: |
| 69 | + def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float: |
| 70 | + h = [(a / b - (a + 1) / (b + 1), a, b) for a, b in classes] |
| 71 | + heapify(h) |
| 72 | + for _ in range(extraStudents): |
| 73 | + _, a, b = heappop(h) |
| 74 | + a, b = a + 1, b + 1 |
| 75 | + heappush(h, (a / b - (a + 1) / (b + 1), a, b)) |
| 76 | + return sum(v[1] / v[2] for v in h) / len(classes) |
57 | 77 | ```
|
58 | 78 |
|
59 | 79 | ### **Java**
|
60 | 80 |
|
61 | 81 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
62 | 82 |
|
63 | 83 | ```java
|
| 84 | +class Solution { |
| 85 | + public double maxAverageRatio(int[][] classes, int extraStudents) { |
| 86 | + PriorityQueue<double[]> pq = new PriorityQueue<>((a, b) -> { |
| 87 | + double x = (a[0] + 1) / (a[1] + 1) - a[0] / a[1]; |
| 88 | + double y = (b[0] + 1) / (b[1] + 1) - b[0] / b[1]; |
| 89 | + return Double.compare(y, x); |
| 90 | + }); |
| 91 | + for (var e : classes) { |
| 92 | + pq.offer(new double[]{e[0], e[1]}); |
| 93 | + } |
| 94 | + while (extraStudents-- > 0) { |
| 95 | + var e = pq.poll(); |
| 96 | + double a = e[0] + 1, b = e[1] + 1; |
| 97 | + pq.offer(new double[]{a, b}); |
| 98 | + } |
| 99 | + double ans = 0; |
| 100 | + while (!pq.isEmpty()) { |
| 101 | + var e = pq.poll(); |
| 102 | + ans += e[0] / e[1]; |
| 103 | + } |
| 104 | + return ans / classes.length; |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### **C++** |
| 110 | + |
| 111 | +```cpp |
| 112 | +class Solution { |
| 113 | +public: |
| 114 | + double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) { |
| 115 | + priority_queue<tuple<double, int, int>> pq; |
| 116 | + for(auto& e : classes) { |
| 117 | + int a = e[0], b = e[1]; |
| 118 | + double x = (double) (a + 1) / (b + 1) - (double) a / b; |
| 119 | + pq.push({x, a, b}); |
| 120 | + } |
| 121 | + while (extraStudents--) { |
| 122 | + auto [_, a, b] = pq.top(); |
| 123 | + pq.pop(); |
| 124 | + a++; |
| 125 | + b++; |
| 126 | + double x = (double) (a + 1) / (b + 1) - (double) a / b; |
| 127 | + pq.push({x, a, b}); |
| 128 | + } |
| 129 | + double ans = 0; |
| 130 | + while (pq.size()) { |
| 131 | + auto [_, a, b] = pq.top(); |
| 132 | + pq.pop(); |
| 133 | + ans += (double) a / b; |
| 134 | + } |
| 135 | + return ans / classes.size(); |
| 136 | + } |
| 137 | +}; |
| 138 | +``` |
64 | 139 |
|
| 140 | +### **Go** |
| 141 | +
|
| 142 | +```go |
| 143 | +func maxAverageRatio(classes [][]int, extraStudents int) float64 { |
| 144 | + pq := hp{} |
| 145 | + for _, e := range classes { |
| 146 | + a, b := e[0], e[1] |
| 147 | + x := float64(a+1)/float64(b+1) - float64(a)/float64(b) |
| 148 | + heap.Push(&pq, tuple{x, a, b}) |
| 149 | + } |
| 150 | + for i := 0; i < extraStudents; i++ { |
| 151 | + e := heap.Pop(&pq).(tuple) |
| 152 | + a, b := e.a+1, e.b+1 |
| 153 | + x := float64(a+1)/float64(b+1) - float64(a)/float64(b) |
| 154 | + heap.Push(&pq, tuple{x, a, b}) |
| 155 | + } |
| 156 | + var ans float64 |
| 157 | + for len(pq) > 0 { |
| 158 | + e := heap.Pop(&pq).(tuple) |
| 159 | + ans += float64(e.a) / float64(e.b) |
| 160 | + } |
| 161 | + return ans / float64(len(classes)) |
| 162 | +} |
| 163 | +
|
| 164 | +type tuple struct { |
| 165 | + x float64 |
| 166 | + a int |
| 167 | + b int |
| 168 | +} |
| 169 | +
|
| 170 | +type hp []tuple |
| 171 | +
|
| 172 | +func (h hp) Len() int { return len(h) } |
| 173 | +func (h hp) Less(i, j int) bool { |
| 174 | + a, b := h[i], h[j] |
| 175 | + return a.x > b.x |
| 176 | +} |
| 177 | +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } |
| 178 | +func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } |
| 179 | +func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } |
65 | 180 | ```
|
66 | 181 |
|
67 | 182 | ### **...**
|
|
0 commit comments