-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxAverageRatio.cpp
More file actions
29 lines (29 loc) · 936 Bytes
/
maxAverageRatio.cpp
File metadata and controls
29 lines (29 loc) · 936 Bytes
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
class Solution {
public: double maxAverageRatio(vector < vector < int >> & classes, int extraStudents) {
auto profit = [ & ](double pass, double total) {
return (pass + 1) / (total + 1) - pass / total;
};
double total = 0;
priority_queue < pair < double, int >> pq;
int n = classes.size();
for (int i = 0; i < n; ++i) {
total += static_cast < double > (classes[i][0]) / classes[i][1];
pq.push({
profit(classes[i][0], classes[i][1]),
i
});
}
while (extraStudents--) {
auto[added_profit, id] = pq.top();
pq.pop();
total += added_profit;
++classes[id][0];
++classes[id][1];
pq.push({
profit(classes[id][0], classes[id][1]),
id
});
}
return total / n;
}
};