-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion77.cpp
33 lines (31 loc) · 880 Bytes
/
Question77.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
77 -> Minimum Cost to Hire K Workers
Solution :
class Solution {
public:
double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int k) {
int n=wage.size();
vector<pair<double,int>>arr;
for(int i=0;i<n;i++){
double ratio=(double)wage[i]/quality[i];
arr.push_back({ratio,quality[i]});
}
sort(arr.begin(),arr.end());
priority_queue<double>pq;
double ans=0;
int sum=0;
for(int j=0;j<k;j++){
pq.push(arr[j].second);
sum+=arr[j].second;
}
ans=sum*arr[k-1].first;
for(int i=k;i<n;i++){
sum+=arr[i].second;
pq.push(arr[i].second);
sum-=pq.top();
pq.pop();
double res=sum*arr[i].first;
ans=min(ans,res);
}
return ans;
}
};