-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1792_MaximumAveragePassStudent.java
More file actions
56 lines (41 loc) · 1.38 KB
/
_1792_MaximumAveragePassStudent.java
File metadata and controls
56 lines (41 loc) · 1.38 KB
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
55
56
import java.util.PriorityQueue;
public class _1792_MaximumAveragePassStudent {
static class ClassInfo{
int pass;
int total;
ClassInfo(int pass, int total){
this.total = total;
this.pass = pass;
}
// gain after adding one extra student
double gain(){
return (double)(pass + 1) / (total + 1) - (double) pass / total;
}
}
public double maxAverageRatio(int[][] classes,int extraStudent){
PriorityQueue<ClassInfo> pq = new PriorityQueue<>((a,b)-> Double.compare(b.gain(), a.gain()));
//push all Classess
for (int[] c : classes) {
pq.add(new ClassInfo(c[0],c[1]));
}
//assign extra student greedly
while (extraStudent-- > 0) {
ClassInfo curr = pq.poll();
curr.pass++;
curr.total++;
pq.add(curr);
}
// calculate final average
double sum = 0.0;
for (ClassInfo c : pq) {
sum += (double) c.pass / c.total;
}
return sum / classes.length;
}
public static void main(String[] args){
_1792_MaximumAveragePassStudent avg = new _1792_MaximumAveragePassStudent();
int[][] classes = {{1,2},{3,5},{2,2}};
int extraStudents = 2;
System.out.println(avg.maxAverageRatio(classes, extraStudents));
}
}