-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1792. Maximum Average Pass Ratio.java
More file actions
83 lines (66 loc) · 3.13 KB
/
1792. Maximum Average Pass Ratio.java
File metadata and controls
83 lines (66 loc) · 3.13 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
1792. Maximum Average Pass Ratio
Solved
Medium
Topics
premium lock icon
Companies
Hint
There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array classes, where classes[i] = [passi, totali]. You know beforehand that in the ith class, there are totali total students, but only passi number of students will pass the exam.
You are also given an integer extraStudents. There are another extraStudents brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents students to a class in a way that maximizes the average pass ratio across all the classes.
The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes.
Return the maximum possible average pass ratio after assigning the extraStudents students. Answers within 10-5 of the actual answer will be accepted.
Example 1:
Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2
Output: 0.78333
Explanation: You can assign the two extra students to the first class. The average pass ratio will be equal to (3/4 + 3/5 + 2/2) / 3 = 0.78333.
Example 2:
Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4
Output: 0.53485
Constraints:
1 <= classes.length <= 105
classes[i].length == 2
1 <= passi <= totali <= 105
1 <= extraStudents <= 105
class Solution {
public double maxAverageRatio(int[][] classes, int extraStudents) {
// Lambda to calculate the gain of adding an extra student
PriorityQueue<double[]> maxHeap = new PriorityQueue<>((a, b) ->
Double.compare(b[0], a[0])
);
for (int[] singleClass : classes) {
int passes = singleClass[0];
int totalStudents = singleClass[1];
double gain = calculateGain(passes, totalStudents);
maxHeap.offer(new double[] { gain, passes, totalStudents });
}
// Distribute extra students
while (extraStudents-- > 0) {
double[] current = maxHeap.poll();
double currentGain = current[0];
int passes = (int) current[1];
int totalStudents = (int) current[2];
maxHeap.offer(
new double[] {
calculateGain(passes + 1, totalStudents + 1),
passes + 1,
totalStudents + 1,
}
);
}
// Calculate the final average pass ratio
double totalPassRatio = 0;
while (!maxHeap.isEmpty()) {
double[] current = maxHeap.poll();
int passes = (int) current[1];
int totalStudents = (int) current[2];
totalPassRatio += (double) passes / totalStudents;
}
return totalPassRatio / classes.length;
}
private double calculateGain(int passes, int totalStudents) {
return (
(double) (passes + 1) / (totalStudents + 1) -
(double) passes / totalStudents
);
}
}