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 extraStudents
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: 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:
Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents
= 4
Output: 0.53485
Constraints:
1 <= classes.length <= 105
classes[i].length == 2
1 <= passi <= totali <= 105
1 <= extraStudents <= 105
class Solution:
def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:
h = [(a / b - (a + 1) / (b + 1), a, b) for a, b in classes]
heapify(h)
for _ in range(extraStudents):
_, a, b = heappop(h)
a, b = a + 1, b + 1
heappush(h, (a / b - (a + 1) / (b + 1), a, b))
return sum(v[1] / v[2] for v in h) / len(classes)
class Solution {
public double maxAverageRatio(int[][] classes, int extraStudents) {
PriorityQueue<double[]> pq = new PriorityQueue<>((a, b) -> {
double x = (a[0] + 1) / (a[1] + 1) - a[0] / a[1];
double y = (b[0] + 1) / (b[1] + 1) - b[0] / b[1];
return Double.compare(y, x);
});
for (var e : classes) {
pq.offer(new double[]{e[0], e[1]});
}
while (extraStudents-- > 0) {
var e = pq.poll();
double a = e[0] + 1, b = e[1] + 1;
pq.offer(new double[]{a, b});
}
double ans = 0;
while (!pq.isEmpty()) {
var e = pq.poll();
ans += e[0] / e[1];
}
return ans / classes.length;
}
}
class Solution {
public:
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
priority_queue<tuple<double, int, int>> pq;
for(auto& e : classes) {
int a = e[0], b = e[1];
double x = (double) (a + 1) / (b + 1) - (double) a / b;
pq.push({x, a, b});
}
while (extraStudents--) {
auto [_, a, b] = pq.top();
pq.pop();
a++;
b++;
double x = (double) (a + 1) / (b + 1) - (double) a / b;
pq.push({x, a, b});
}
double ans = 0;
while (pq.size()) {
auto [_, a, b] = pq.top();
pq.pop();
ans += (double) a / b;
}
return ans / classes.size();
}
};
func maxAverageRatio(classes [][]int, extraStudents int) float64 {
pq := hp{}
for _, e := range classes {
a, b := e[0], e[1]
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
heap.Push(&pq, tuple{x, a, b})
}
for i := 0; i < extraStudents; i++ {
e := heap.Pop(&pq).(tuple)
a, b := e.a+1, e.b+1
x := float64(a+1)/float64(b+1) - float64(a)/float64(b)
heap.Push(&pq, tuple{x, a, b})
}
var ans float64
for len(pq) > 0 {
e := heap.Pop(&pq).(tuple)
ans += float64(e.a) / float64(e.b)
}
return ans / float64(len(classes))
}
type tuple struct {
x float64
a int
b int
}
type hp []tuple
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool {
a, b := h[i], h[j]
return a.x > b.x
}
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) }
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }