-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path1792. Maximum Average Pass Ratio
49 lines (38 loc) · 1.74 KB
/
1792. 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
class Solution {
public:
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
int classesLen = classes.size();
// pq to know which class will increase the avg the most
priority_queue<pair<double, int>> valueQueue;
double finalAvg = 0;
double localAvg = 0;
// going through all classes adding them to finalAvg and
// placing their avg increase into the pq if we were to add
// an extra student there
for (int i = 0; i < classesLen; ++i) {
localAvg = static_cast<double>(classes[i][0]) / classes[i][1];
valueQueue.push({(static_cast<double>(classes[i][0] + 1) / (classes[i][1] + 1)) - localAvg, i});
finalAvg += localAvg;
}
// devide the avg once after the loop
finalAvg /= classesLen;
// while there are students to distribute we go through the pq
while (extraStudents > 0) {
// we take the class with highest avg inc value
auto top = valueQueue.top();
valueQueue.pop();
// add the value / classLen to the finalAvg
finalAvg += top.first / classesLen;
// we add a student to that class
++classes[top.second][1];
++classes[top.second][0];
// recalculate that class avg inc if we were to add an
// extra student
localAvg = static_cast<double>(classes[top.second][0]) / classes[top.second][1];
valueQueue.push({(static_cast<double>(classes[top.second][0] + 1) / (classes[top.second][1] + 1)) - localAvg, top.second});
// decrement the extraStudents left
--extraStudents;
}
return finalAvg;
}
};