Skip to content

Commit 493cf81

Browse files
authored
Create 1792. Maximum Average Pass Ratio (#662)
2 parents 320524b + e527b91 commit 493cf81

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

1792. Maximum Average Pass Ratio

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public:
3+
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
4+
int classesLen = classes.size();
5+
6+
// pq to know which class will increase the avg the most
7+
priority_queue<pair<double, int>> valueQueue;
8+
double finalAvg = 0;
9+
double localAvg = 0;
10+
11+
12+
// going through all classes adding them to finalAvg and
13+
// placing their avg increase into the pq if we were to add
14+
// an extra student there
15+
for (int i = 0; i < classesLen; ++i) {
16+
localAvg = static_cast<double>(classes[i][0]) / classes[i][1];
17+
valueQueue.push({(static_cast<double>(classes[i][0] + 1) / (classes[i][1] + 1)) - localAvg, i});
18+
finalAvg += localAvg;
19+
}
20+
21+
// devide the avg once after the loop
22+
finalAvg /= classesLen;
23+
24+
25+
// while there are students to distribute we go through the pq
26+
while (extraStudents > 0) {
27+
// we take the class with highest avg inc value
28+
auto top = valueQueue.top();
29+
valueQueue.pop();
30+
31+
// add the value / classLen to the finalAvg
32+
finalAvg += top.first / classesLen;
33+
34+
// we add a student to that class
35+
++classes[top.second][1];
36+
++classes[top.second][0];
37+
38+
// recalculate that class avg inc if we were to add an
39+
// extra student
40+
localAvg = static_cast<double>(classes[top.second][0]) / classes[top.second][1];
41+
valueQueue.push({(static_cast<double>(classes[top.second][0] + 1) / (classes[top.second][1] + 1)) - localAvg, top.second});
42+
43+
// decrement the extraStudents left
44+
--extraStudents;
45+
}
46+
47+
return finalAvg;
48+
}
49+
};

0 commit comments

Comments
 (0)