diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/max_average_pass_ration.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/max_average_pass_ration.cpp')
-rw-r--r-- | other/max_average_pass_ration.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/other/max_average_pass_ration.cpp b/other/max_average_pass_ration.cpp new file mode 100644 index 0000000..e9ebb25 --- /dev/null +++ b/other/max_average_pass_ration.cpp @@ -0,0 +1,57 @@ +// 1792. Maximum Average Pass Ratio +#include "leetcode.h" + +/* + * there is a school that has classes of student and each class will be having a + * final exam. you are given a 2d integer array 'classes', where 'classes[i] = + * [pass_i, total_i]'. you know beforehand that in the i'th class, there are + * 'total_i' total students, but only 'pass_i' number of students will pass the + * exam. you are also given an integer 'extraStudents'. there are another + * 'extraStudents' brilliant students that are guaranteed to pass the exam of + * any class they are assigned to. you want to assign each of the + * 'extraStudents' to a class in a way that maximises the average pass ration + * across all classes. return the maximum possible average pass ratio after + * assigning the 'extraStudents'. answers within '10^-5' of the actual answer + * will be accepted. + */ + +struct cmp { + bool operator()(pair<int, int> a, pair<int, int> b) { + double aa = + (a.first + 1) / (double)(a.second + 1) - (a.first) / (double)a.second; + double bb = + (b.first + 1) / (double)(b.second + 1) - (b.first) / (double)b.second; + return aa < bb; + } +}; + +class Solution { +public: + double maxAverageRatio(vector<vector<int>> &classes, int extraStudents) { + double acc = 0; + priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq; + for (vector<int> i : classes) + pq.push(make_pair(i[0], i[1])); + while (extraStudents--) { + pair<int, int> curr = pq.top(); + pq.pop(); + curr.first++; + curr.second++; + pq.push(curr); + } + while (!pq.empty()) { + pair<int, int> curr = pq.top(); + pq.pop(); + acc += curr.first / (double)curr.second; + } + return acc / (double)classes.size(); + } +}; + +int main() { + Solution obj; + vvd c1 = {{1, 2}, {3, 5}, {2, 2}}, + c2 = {{2, 4}, {3, 9}, {4, 5}, {2, 10}}; + printf("%f\n", obj.maxAverageRatio(c1, 2)); // expect: 0.78333 + printf("%f\n", obj.maxAverageRatio(c2, 4)); // expect: 0.53485 +} |