-
Notifications
You must be signed in to change notification settings - Fork 0
/
857. Minimum Cost to Hire K Workers.cpp
41 lines (31 loc) · 1.17 KB
/
857. 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
40
41
class Solution {
public:
double mincostToHireWorkers(vector<int>& quality, vector<int>& wage,int k) {
int n = quality.size();
vector<pair<double, int>> worker_ratio(n);
for (int worker = 0; worker < n; worker++) {
worker_ratio[worker] = make_pair(
(double)wage[worker] / quality[worker], quality[worker]);
}
sort(begin(worker_ratio), end(worker_ratio));
priority_queue<int, vector<int>> pq;
double sum_quality = 0;
for (int i = 0; i < k; i++) {
pq.push(worker_ratio[i].second);
sum_quality += worker_ratio[i].second;
}
double managerRatio = worker_ratio[k - 1].first;
double result = managerRatio * sum_quality;
for (int manager = k; manager < n; manager++) {
managerRatio = worker_ratio[manager].first;
pq.push(worker_ratio[manager].second);
sum_quality += worker_ratio[manager].second;
if (pq.size() > k) {
sum_quality -= pq.top();
pq.pop();
}
result = min(result, managerRatio * sum_quality);
}
return result;
}
};