100% working Java Code , Fixed Editorial Code. Java Double Precision Problem
Complexity

Time complexity:
O(nlogn)


Space complexity:
O(n)

Code


# Complexity
- Time complexity:
    $$O(nlogn)$$

- Space complexity:
 $$O(n)$$

# Code
```java []
import java.util.PriorityQueue;

class Solution {
    private double f(int p, int t) {
        return ((double) (p + 1) / (t + 1)) - ((double) p / t);
    }

    public double maxAverageRatio(int[][] classes, int extraStudents) {
        // Max heap to prioritize classes with the highest gain
        PriorityQueue<double[]> pq = new PriorityQueue<>(
            (a, b) -> Double.compare(b[0], a[0])
        );

        // Initialize the priority queue with the current gain and class data
        for (int[] x : classes) {
            int p = x[0];
            int t = x[1];
            double gain = f(p, t);
            pq.add(new double[]{gain, p, t});
        }

        // Distribute extra students
        while (extraStudents-- > 0) {
            double[] curr = pq.poll();
            int p = (int) curr[1];
            int t = (int) curr[2];
            pq.add(new double[]{f(p + 1, t + 1), p + 1, t + 1});
        }

        // Calculate the total average ratio
        double sol = 0;
        while (!pq.isEmpty()) {
            double[] curr = pq.poll();
            int p = (int) curr[1];
            int t = (int) curr[2];
            sol += (double) p / t;
        }

        return sol / classes.length;
    }
}

```
['Array', 'Greedy', 'Heap (Priority Queue)', 'C++', 'Java', 'MySQL']