-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMaxAveragePassRatio.cpp
51 lines (44 loc) · 1.45 KB
/
MaxAveragePassRatio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Problem: https://leetcode.com/problems/maximum-average-pass-ratio/
#include<vector>
#include<priority_queue>
using namespace std;
struct PassRatio {
PassRatio(int np, int ns) : _num_pass(np), _num_students(ns) {
_increment = ((double)(np + 1) / (double)(ns + 1)) - ((double)np / (double)ns);
}
void increment() {
++_num_students;
++_num_pass;
_increment = ((double)(_num_pass + 1) / (double)(_num_students + 1)) -
((double)_num_pass / (double)_num_students);
}
int _num_students;
int _num_pass;
double _increment;
};
struct ComparePassRatio {
inline bool operator()(const PassRatio& l, const PassRatio& r) {
return l._increment < r._increment;
}
};
class MaxAveragePassRatio {
public:
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
priority_queue<PassRatio, vector<PassRatio>, ComparePassRatio> pq;
for (int i = 0; i < classes.size(); ++i) {
pq.push(PassRatio(classes[i][0], classes[i][1]));
}
while (extraStudents > 0) {
PassRatio pr = pq.top(); pq.pop();
pr.increment();
pq.push(pr);
--extraStudents;
}
double sum = 0;
while (not pq.empty()) {
PassRatio pr = pq.top(); pq.pop();
sum += (double) pr._num_pass / (double) pr._num_students;
}
return (sum / (double)classes.size());
}
};