-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-q-18.java
More file actions
29 lines (24 loc) · 747 Bytes
/
leet-q-18.java
File metadata and controls
29 lines (24 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public double maxAverageRatio(int[][] classes, int extraStudents) {
PriorityQueue<int[]> pq = new PriorityQueue<>(
(a, b) -> Double.compare(gain(b[0], b[1]), gain(a[0], a[1]))
);
for (int[] c : classes) {
pq.add(new int[]{c[0], c[1]});
}
while (extraStudents-- > 0) {
int[] top = pq.poll();
top[0]++;
top[1]++;
pq.add(top);
}
double sum = 0.0;
for (int[] c : pq) {
sum += (double)c[0] / c[1];
}
return sum / classes.length;
}
private double gain(int pass, int total) {
return (double)(pass + 1) / (total + 1) - (double)pass / total;
}
}