-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum Cost to Hire K Workers.cpp
39 lines (32 loc) · 1.16 KB
/
Minimum Cost to Hire K Workers.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
class Solution {
public:
double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int k) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
vector<pair<double, int>> ratio;
int n = quality.size();
for (int i = 0; i < n; ++i) {
ratio.emplace_back(static_cast<double>(wage[i]) / quality[i], i);
}
sort(begin(ratio), end(ratio));
priority_queue<int> maxHeap;
int qualitySum = 0;
double maxRate = 0.0;
for (int i = 0; i < k; ++i) {
qualitySum += quality[ratio[i].second];
maxRate = max(maxRate, ratio[i].first);
maxHeap.push(quality[ratio[i].second]);
}
double res = maxRate * qualitySum;
for (int i = k; i < n; ++i) {
maxRate = max(maxRate, ratio[i].first);
qualitySum -= maxHeap.top();
maxHeap.pop();
qualitySum += quality[ratio[i].second];
maxHeap.push(quality[ratio[i].second]);
res = min(res, maxRate * qualitySum);
}
return res;
}
};