Java & Python3 Solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\nimport java.util.PriorityQueue;\n\npublic class Solution {\n    public double maxAverageRatio(int[][] classes, int extraStudents) {\n        // Create a max-heap to store the classes based on the potential increase in pass ratio\n        PriorityQueue<ClassInfo> maxHeap = new PriorityQueue<>((a, b) -> {\n            // Compare based on the potential increase in pass ratio\n            return Double.compare(b.increase, a.increase);\n        });\n\n        // Calculate the initial pass ratios and their potential increases\n        for (int[] classInfo : classes) {\n            int passi = classInfo[0];\n            int totali = classInfo[1];\n            double currentRatio = (double) passi / totali;\n            double newRatio = (double) (passi + 1) / (totali + 1);\n            double increase = newRatio - currentRatio;\n            maxHeap.offer(new ClassInfo(passi, totali, increase));\n        }\n\n        // Distribute the extra students\n        for (int i = 0; i < extraStudents; i++) {\n            ClassInfo classInfo = maxHeap.poll();\n            classInfo.passi += 1;\n            classInfo.totali += 1;\n            double currentRatio = (double) classInfo.passi / classInfo.totali;\n            double newRatio = (double) (classInfo.passi + 1) / (classInfo.totali + 1);\n            classInfo.increase = newRatio - currentRatio;\n            maxHeap.offer(classInfo);\n        }\n\n        // Calculate the final average pass ratio\n        double totalPassRatio = 0;\n        for (ClassInfo classInfo : maxHeap) {\n            totalPassRatio += (double) classInfo.passi / classInfo.totali;\n        }\n\n        return totalPassRatio / classes.length;\n    }\n\n    // Helper class to store class information\n    private static class ClassInfo {\n        int passi;\n        int totali;\n        double increase;\n\n        ClassInfo(int passi, int totali, double increase) {\n            this.passi = passi;\n            this.totali = totali;\n            this.increase = increase;\n        }\n    }\n\n    // Example usage\n    public static void main(String[] args) {\n        Solution solution = new Solution();\n        \n        int[][] classes1 = {{1, 2}, {3, 5}, {2, 2}};\n        int extraStudents1 = 2;\n        System.out.println(solution.maxAverageRatio(classes1, extraStudents1));  // Output: 0.78333\n\n        int[][] classes2 = {{2, 4}, {3, 9}, {4, 5}, {2, 10}};\n        int extraStudents2 = 4;\n        System.out.println(solution.maxAverageRatio(classes2, extraStudents2));  // Output: 0.53485\n    }\n}\n```\n```python3 []\nimport heapq\n\nclass Solution:\n    def maxAverageRatio(self, classes, extraStudents):\n        # Create a max-heap based on the potential increase in pass ratio\n        max_heap = []\n        \n        # Populate the heap with the initial classes and their potential increases\n        for passi, totali in classes:\n            # Calculate the current pass ratio and the potential increase\n            current_ratio = passi / totali\n            # Calculate the increase in pass ratio if we add one extra student\n            increase = (passi + 1) / (totali + 1) - current_ratio\n            # Push the negative of the increase to create a max-heap\n            heapq.heappush(max_heap, (-increase, passi, totali))\n        \n        # Assign extra students\n        for _ in range(extraStudents):\n            # Get the class with the maximum increase in pass ratio\n            increase, passi, totali = heapq.heappop(max_heap)\n            increase = -increase  # Convert back to positive\n            # Assign one extra student to this class\n            passi += 1\n            totali += 1\n            # Recalculate the new increase and push it back into the heap\n            new_increase = (passi + 1) / (totali + 1) - (passi / totali)\n            heapq.heappush(max_heap, (-new_increase, passi, totali))\n        \n        # Calculate the final average pass ratio\n        total_pass_ratio = 0\n        for _, passi, totali in max_heap:\n            total_pass_ratio += passi / totali\n        \n        return total_pass_ratio / len(classes)\n\n# Example usage\nsolution = Solution()\nclasses1 = [[1, 2], [3, 5], [2, 2]]\nextraStudents1 = 2\nprint(solution.maxAverageRatio(classes1, extraStudents1))  # Output: 0.78333\n\nclasses2 = [[2, 4], [3, 9], [4, 5], [2, 10]]\nextraStudents2 = 4\nprint(solution.maxAverageRatio(classes2, extraStudents2))  # Output: 0.53485\n```\n
['Array', 'Java', 'Python3']