From e527b9131ef8db8a71238219378b4305eccc9014 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 15 Dec 2024 23:50:41 +0530 Subject: [PATCH] Create 1792. Maximum Average Pass Ratio --- 1792. Maximum Average Pass Ratio | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 1792. Maximum Average Pass Ratio 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; + } +};