[Java] 🎄 Priority Queue ✨ Inline comments for clarity 😍
IntuitionGreedily increase the pass rate for the classes where adding just 1 student increases the pass rate more than in others.Complexity

Time complexity:
O(
# Intuition\nGreedily increase the pass rate for the classes where adding just 1 student increases the pass rate more than in others.\n\n# Complexity\n- Time complexity:\n$$O(n log n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```java []\nclass Solution {\n    public double maxAverageRatio(int[][] classes, int extraStudents) {\n        // we will be greedily picking the class with the largest possible increase in a pass rate\n        Queue<int[]> pq = new PriorityQueue<>((a, b) -> Double.compare(diff(b), diff(a)));\n        for (int[] cl: classes) {\n            pq.add(cl);\n        }\n        // so now we are putting the extra students every time to the class \n        // where they will contribute to the pass rate mostly. See diff(int[] a) for details\n        for (int i = 0; i < extraStudents; i++) {\n            int[] next = pq.poll();\n            next[0]++;\n            next[1]++;\n            // so we added an extra student and returned the class back to the PriorityQueue\n            // in the next move it may be another class that needs our contribution in a form of an ideal student :)\n            pq.add(next);\n        }\n        double sum = 0.0;\n        // we can also define the number of classes as just classes.length\n        int count = pq.size();\n        while (!pq.isEmpty()) {\n            int[] next = pq.poll();\n            sum += (double) next[0] / (double) next[1];\n        }\n        return sum / count;\n    }\n\n    // calculate how the pass rate will increase  for the class if we add an ideal student\n    // that is, (pass + 1) / (total + 1) - pass / total\n    private double diff(int[] a) {\n        double aCurr = (double)a[0] / (double) a[1];\n        double aAdd = (double)(a[0] + 1) / (double) (a[1] + 1);\n        return aAdd - aCurr;        \n    }\n}\n```
['Java']