-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum Average Pass Ratio
54 lines (45 loc) · 1.21 KB
/
Maximum Average Pass Ratio
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public double maxAverageRatio(int[][] classes, int extraStudents) {
PriorityQueue<ClassRecord> pq = new PriorityQueue<>(new Compare());
for(int[] cl : classes)
pq.add(new ClassRecord(cl));
ClassRecord cl;
while(extraStudents-- > 0)
pq.add(pq.remove().addOneStudent());
double sum = 0;
while(!pq.isEmpty()){
cl = pq.remove();
sum += (double)cl.pass / cl.total;
}
return sum / classes.length;
}
}
class ClassRecord{
int pass;
int total;
double inc;
public ClassRecord(int[] array){
pass = array[0];
total = array[1];
inc = getIncrement();
}
public ClassRecord addOneStudent(){
pass++;
total++;
inc = getIncrement();
return this;
}
private double getIncrement(){
return (pass + 1.0) / (total + 1) - (double)pass / total;
}
}
class Compare implements Comparator<ClassRecord>{
public int compare(ClassRecord a, ClassRecord b){
if(a.inc < b.inc)
return 1;
else if(a.inc > b.inc)
return -1;
else
return 0;
}
}