Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions 1792. Maximum Average Pass Ratio
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
};
Loading