Problem

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 _themaximum possible average pass ratio after assigning the _extraStudents students. Answers within 10^-5 of the actual answer will be accepted.

Examples

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

Solution

Method 1 - Using Max heap

To solve this problem, we can use a max-heap to keep track of the classes where adding an extra student would yield the maximum increase in the pass ratio. By repeatedly assigning each extra student to the class that benefits the most, we can ensure that the overall average pass ratio is maximized.

Steps

  1. Compute the initial pass ratio for each class.
  2. Calculate the potential benefit (increase in pass ratio) of adding one more student to each class.
  3. Use a max-heap to always favor adding students to the class with the highest potential benefit.
  4. Add extraStudents to the classes, updating the heap each time as the conditions change.
  5. Finally, calculate the average pass ratio after all students have been assigned.

Code

Java
class Solution {
    public double maxAverageRatio(int[][] classes, int extraStudents) {
        PriorityQueue<double[]> maxHeap = new PriorityQueue<>((a, b) -> Double.compare(b[0], a[0]));
        for (int[] c : classes) {
            int pass = c[0], total = c[1];
            double benef = benefit(pass, total);
            maxHeap.offer(new double[]{benef, pass, total});
        }
        
        while (extraStudents-- > 0) {
            double[] top = maxHeap.poll();
            int pass = (int)top[1] + 1, total = (int)top[2] + 1;
            double benef = benefit(pass, total);
            maxHeap.offer(new double[]{benef, pass, total});
        }
        
        double ans = 0;
        while (!maxHeap.isEmpty()) {
            double[] c = maxHeap.poll();
            ans += ((int)c[1]) / c[2];
        }
        return ans / classes.length;
    }

    private double benefit(int pass, int total) {
        return ((double)(pass + 1) / (total + 1)) - ((double)pass / total);
    }
}
Python
class Solution:
    def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:
        heap = []
        
        def benefit(p: int, t: int) -> float:
            return (p + 1) / (t + 1) - p / t
        
        for passi, totali in classes:
            heappush(heap, (-benefit(passi, totali), passi, totali))
        
        for _ in range(extraStudents):
            b, p, t = heappop(heap)
            p, t  = p + 1, t + 1
            heappush(heap, (-benefit(p, t), p, t))
        
        ans = sum(p / t for _, p, t in heap)
        return ans / len(classes)

Complexity

  • ⏰ Time complexity: O(extraStudents * log n)
    • Initializing the heap takes O(n) where n is the number of classes.
    • Each operation to add an extra student and adjust the heap takes O(log n).
    • As we perform extraStudents operations, the overall time complexity is O(extraStudents * log n).
  • 🧺 Space complexity: O(n), due to the storage required for the heap.