Skip to content

Commit 032d374

Browse files
committed
Time: 539 ms (15.19%), Space: 75.8 MB (90.24%) - LeetHub
1 parent 5b48eec commit 032d374

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public double maxAverageRatio(int[][] classes, int k) {
3+
double res=0;
4+
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> {
5+
double xDiff = (double)(x[0]+1)/(x[1]+1) - (double)x[0]/x[1];
6+
double yDiff = (double)(y[0]+1)/(y[1]+1) - (double)y[0]/y[1];
7+
return xDiff > yDiff? -1 : 1;
8+
});
9+
10+
for(int[] c : classes){
11+
pq.offer(c);
12+
}
13+
while(k-->0){
14+
int[] c = pq.poll();
15+
c[0]++;
16+
c[1]++;
17+
pq.add(c);
18+
}
19+
20+
while(!pq.isEmpty()){
21+
int[] c = pq.poll();
22+
res += (double)c[0]/c[1];
23+
}
24+
return res/classes.length;
25+
}
26+
}

0 commit comments

Comments
 (0)