Advertisement
nikunjsoni

857

May 22nd, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int k) {
  4.         vector<pair<double, int>> workers;
  5.         int n = quality.size();
  6.        
  7.         // Calculate the wage/qual ratio for each worker and sort them as per ratio.
  8.         for(int i=0; i<n; i++)
  9.             workers.push_back({double(wage[i])/quality[i], quality[i]});
  10.         sort(workers.begin(), workers.end());
  11.        
  12.         // Pick k min quality workers and pay as per highest ratio seen so far.
  13.         priority_queue<int> pq;
  14.         double qsum = 0, ans = DBL_MAX;
  15.         for(auto worker: workers){
  16.             qsum += worker.second;
  17.             pq.push(worker.second);
  18.             if(pq.size() > k){
  19.                 qsum -= pq.top();
  20.                 pq.pop();
  21.             }
  22.             if(pq.size() == k)
  23.                 ans = min(ans, qsum*worker.first);
  24.         }
  25.         return ans;
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement