LeetCode 1792. Maximum Average Pass Ratio

35 阅读1分钟

🔗 leetcode.com/problems/ma…

题目

  • 给若干班级,以及每个班级的学生人数,以及通过考试的学生人数
  • 给若干必过学生人数,添加到班级中,使得班级的平均通过率最大
  • 返回最大平均通过率的值

思路

  • 优先队列,不过优先队列的判断条件是,这个学生加在哪个班级,通过率提升的比例最高
  • 注意 int 需要转成 double 去算除法

代码

class Solution {
public:
    double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
        int all_pass = 0;
        auto cmp = [](pair<int, int> left, pair<int, int> right) {
		    return ( 
                ((double)left.first + 1) / ((double)left.second + 1) - 
                    (double)left.first/left.second
                ) 
                < 
                (
                    ((double)right.first + 1) / ((double)right.second + 1) - 
                    (double)right.first/right.second
                ) ;
        };
        std::priority_queue<pair<int, int>, std::vector<pair<int, int>>, decltype(cmp)> hp;
        for (auto& item : classes) {
            int pass = item[0], total = item[1];
            if (pass == total) {
                all_pass++;
                continue;
            }
            hp.push(make_pair(pass, total));
        }

        if (hp.empty()) return 1;
        while (extraStudents--) {
            auto p = hp.top(); hp.pop();
            int pass = p.first + 1;
            int total = p.second + 1;
            hp.push(make_pair(pass, total));
        }

        double sum = 0;
        int count = 0;
        int size = hp.size();
        while (hp.empty() == false) {
            auto p = hp.top(); hp.pop();
            double pass = p.first;
            double total = p.second;
            sum += pass/total;
            count++;
        }
        sum += all_pass;
        count += all_pass;
        return sum / count;
    }
};