Java solution with detailed explanation | Solved using Max-Heap | Time complexity: O(nlogn) 
Problem
Each class has some students who passed the exam and some who didn't.
You want to improve the school's average pass rate:

Pass rate of a class = Studen
# Problem\n\n1. Each class has some students who passed the exam and some who didn\'t.\n2. You want to improve the school\'s average pass rate:\n    - Pass rate of a class = Students who passed \xF7 Total students in that class.\n    - Average pass rate = Sum of pass rates of all classes \xF7 Number of classes.\n3. You have some extra "super students" who will always pass the exam if they join a class.\n4. You can put these extra students into any class, one at a time.\n5. Your job is to decide which classes to put these extra students into, so the average pass rate becomes as high as possible.\n\n# Intution\n\n- Adding students to a class increases its pass rate, but:\n    - The first extra student you add makes the biggest improvement in the pass rate.\n    - The more students you add to the same class, the less improvement you see.\n- So, to get the best results, you should:\n    - Always give the next extra student to the class where it helps the most.\n\n# Plan \n\nTo solve this step by step:\n\n1. Find out how much each class improves if you add one extra student.\n2. Always pick the class with the biggest improvement and give it one extra student.\n3. Repeat this until you\'ve assigned all the extra students.\n4. Calculate the average pass rate after all extra students are assigned.\n\n# Measuring "Improvement"?\nThe improvement in the pass rate for a class if you add one extra student is:\n\n![Screenshot 2024-12-15 225442.png](https://assets.leetcode.com/users/images/4ea13591-5aab-44d7-b179-931cc90685b7_1734283591.60347.png)\n\n- This tells you how much better the class\'s pass rate will be after adding a student.\n- We call this "improvement" the gain.\n# Solving with a Max-Heap\nNow, to assign the extra students smartly:\n\n1. Make a list of all classes and calculate their gain for adding one student.\n2. Put all these gains in a "max-heap." A max-heap is a structure where you can always quickly find the class with the highest gain.\n3. For each extra student:\n    - Take the class with the highest gain from the heap.\n    - Add the student to that class.\n    - Recalculate the new gain for that class and put it back in the heap.\n4. After all extra students are assigned, calculate the final average pass rate.\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public double maxAverageRatio(int[][] classes, int extraStudents) {\n        // Max-Heap to store the classes based on the gain in pass ratio\n        PriorityQueue<double[]> maxHeap = new PriorityQueue<>(\n            (a, b) -> Double.compare(b[0], a[0]) \n        );\n\n        // fill the heap with initial values\n        for (int[] c : classes) {\n            int passi = c[0], totali = c[1];\n            double gain = calculateGain(passi, totali);\n            maxHeap.offer(new double[] {gain, passi, totali});\n        }\n\n        // Assign extra students\n        while (extraStudents > 0) {\n            // Extract the class with the maximum gain\n            double[] top = maxHeap.poll();\n            double gain = top[0];\n            int passi = (int) top[1];\n            int totali = (int) top[2];\n\n            // Update the class with additional student\n            passi++;\n            totali++;\n\n            // Recompute the gain and reinsert into the heap\n            double newGain = calculateGain(passi, totali);\n            maxHeap.offer(new double[] {newGain, passi, totali});\n\n            // Decrement extra students\n            extraStudents--;\n        }\n\n        // Compute the final average pass ratio\n        double totalRatio = 0.0;\n        for (double[] entry : maxHeap) {\n            int passi = (int) entry[1];\n            int totali = (int) entry[2];\n            totalRatio += (double) passi / totali;\n        }\n\n        return totalRatio / classes.length;\n    }\n\n    // Helper method to compute the gain of adding one student\n    private double calculateGain(int passi, int totali) {\n        double currentRatio = (double) passi / totali;\n        double newRatio = (double) (passi + 1) / (totali + 1);\n        return newRatio - currentRatio;\n    }\n}\n```
['Math', 'Greedy', 'Heap (Priority Queue)', 'Java']