exmkg

Untitled

Oct 27th, 2024
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. class Solution {
  2.     public double maxAverageRatio(int[][] classes, int extraStudents) {
  3.         PriorityQueue<double[]> maxHeap = new PriorityQueue<>(
  4.             Comparator.comparingDouble(o -> -o[0])
  5.         ); // Max heap compared by first value in decreasing order.
  6.         for (int[] c : classes) {
  7.             double a = c[0], b = c[1];
  8.             maxHeap.offer(new double[]{profit(a, b), a, b});
  9.         }
  10.         while (extraStudents > 0) {
  11.             double[] top = maxHeap.poll();
  12.             double a = top[1], b = top[2];
  13.             maxHeap.offer(new double[]{profit(a + 1, b + 1), a + 1, b + 1});
  14.             extraStudents--;
  15.         }
  16.         double ans = 0.0;
  17.         while (!maxHeap.isEmpty()) {
  18.             double[] top = maxHeap.poll();
  19.             double a = top[1], b = top[2];
  20.             ans += a / b;
  21.         }
  22.         return ans / classes.length;
  23.     }
  24.  
  25.     double profit(double a, double b) {
  26.         return (a + 1) / (b + 1) - a / b;
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment