1792. Maximum Average Pass Ratio

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

Approach 01:

class Solution {
 public:
  double maxAverageRatio(vector<vector<int>>& classData, int extraStudents) {
    // (extra pass ratio, pass, total)
    priority_queue<tuple<double, int, int>> maxHeap;

    for (const vector<int>& classInfo : classData) {
      const int passedStudents = classInfo[0];
      const int totalStudents = classInfo[1];
      maxHeap.emplace(extraPassRatio(passedStudents, totalStudents), passedStudents, totalStudents);
    }

    for (int i = 0; i < extraStudents; ++i) {
      const auto [_, passedStudents, totalStudents] = maxHeap.top();
      maxHeap.pop();
      maxHeap.emplace(extraPassRatio(passedStudents + 1, totalStudents + 1), passedStudents + 1, totalStudents + 1);
    }

    double totalRatio = 0;

    while (!maxHeap.empty()) {
      const auto [_, passedStudents, totalStudents] = maxHeap.top();
      maxHeap.pop();
      totalRatio += passedStudents / static_cast<double>(totalStudents);
    }

    return totalRatio / classData.size();
  }

 private:
  // Returns the extra pass ratio if a brilliant student joins.
  double extraPassRatio(int passedStudents, int totalStudents) {
    return (passedStudents + 1) / static_cast<double>(totalStudents + 1) -
           passedStudents / static_cast<double>(totalStudents);
  }
};
import heapq

class Solution:
    def maxAverageRatio(self, classData: list[list[int]], extraStudents: int) -> float:
        # (extra pass ratio, pass, total)
        maxHeap = []

        for classInfo in classData:
            passedStudents = classInfo[0]
            totalStudents = classInfo[1]
            heapq.heappush(maxHeap, (-self.extraPassRatio(passedStudents, totalStudents), passedStudents, totalStudents))

        for _ in range(extraStudents):
            _, passedStudents, totalStudents = heapq.heappop(maxHeap)
            heapq.heappush(maxHeap, (-self.extraPassRatio(passedStudents + 1, totalStudents + 1), passedStudents + 1, totalStudents + 1))

        totalRatio = 0

        while maxHeap:
            _, passedStudents, totalStudents = heapq.heappop(maxHeap)
            totalRatio += passedStudents / totalStudents

        return totalRatio / len(classData)

    def extraPassRatio(self, passedStudents: int, totalStudents: int) -> float:
        return (passedStudents + 1) / (totalStudents + 1) - passedStudents / totalStudents

Time Complexity

  • Building the Max Heap:
    • The function initializes the max heap by iterating over each class in the classData array, which contains \(n\) elements. For each class, we calculate the extra pass ratio, which is an \(O(1)\) operation. Hence, the time complexity for this step is \(O(n)\).
  • Inserting Extra Students:
    • For each extra student (up to \(k\) extra students), the heap operations (pop and push) take \(O(\log n)\) time. This step is repeated \(k\) times, resulting in a total time complexity of \(O(k \log n)\).
  • Summing the Ratios:
    • The final step involves iterating over the heap to sum up the ratios, which takes \(O(n)\) time since there are \(n\) classes.
  • Overall Time Complexity:

    The total time complexity is \(O(n + k \log n)\), where \(n\) is the number of classes and \(k\) is the number of extra students. If \(k\) is small compared to \(n\), this simplifies to \(O(n + k \log n)\).

Space Complexity

  • Max Heap:
    • The max heap stores \(n\) elements (one for each class), each containing three values: the extra pass ratio, the number of passed students, and the total number of students. Thus, the space complexity for the heap is \(O(n)\).
  • Other Variables:
    • Variables like totalRatio and loop variables use \(O(1)\) space.
  • Overall Space Complexity:

    The space complexity is \(O(n)\), dominated by the max heap storing class information.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top