diff --git a/1792. Maximum Average Pass Ratio b/1792. Maximum Average Pass Ratio new file mode 100644 index 0000000..44da1ef --- /dev/null +++ b/1792. Maximum Average Pass Ratio @@ -0,0 +1,49 @@ +class Solution { +public: + double maxAverageRatio(vector>& classes, int extraStudents) { + int classesLen = classes.size(); + + // pq to know which class will increase the avg the most + priority_queue> 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(classes[i][0]) / classes[i][1]; + valueQueue.push({(static_cast(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(classes[top.second][0]) / classes[top.second][1]; + valueQueue.push({(static_cast(classes[top.second][0] + 1) / (classes[top.second][1] + 1)) - localAvg, top.second}); + + // decrement the extraStudents left + --extraStudents; + } + + return finalAvg; + } +};