There are N workers. The i-th worker has a quality quality[i] and a minimum wage expectation wage[i]. Now you want to hire exactly K workers to form a paid group. When hiring a group of K workers, you must pay them according to the following rules:
- Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.
- Every worker in the paid group must be paid at least their minimum wage expectation.
Return the least amount of money needed to form a paid group satisfying the above conditions.
Table of Contents
Sample Input/Output:
Let’s consider a sample input:
quality = [10, 20, 5]
wage = [70, 50, 30]
K = 2
Expected Output:
105.0
Explanation:
For the given input, if we hire the first worker with quality 10 and wage expectation 70, then we must hire the third worker with quality 5 (since their wage/quality ratio is the smallest among all workers) to form a paid group of size K = 2. The total cost would be (70 + 30) * (5 / 10) = 105.0.
Time and Space Complexity:
The time complexity of the solution will be O(N log N), where N is the number of workers. This is because we sort the workers based on their wage/quality ratio.
The space complexity will be O(N) to store the sorted array and a priority queue.
C++ Solution:
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
double minCostToHireWorkers(vector<int>& quality, vector<int>& wage, int K) {
int N = quality.size();
vector<pair<double, int>> workers;
// Calculate wage/quality ratio for each worker
for (int i = 0; i < N; ++i) {
workers.push_back({static_cast<double>(wage[i]) / quality[i], quality[i]});
}
// Sort workers by wage/quality ratio
sort(workers.begin(), workers.end());
priority_queue<int> maxHeap;
int sumQuality = 0;
double result = DBL_MAX;
for (const auto& worker : workers) {
sumQuality += worker.second;
maxHeap.push(worker.second);
if (maxHeap.size() > K) {
sumQuality -= maxHeap.top();
maxHeap.pop();
}
if (maxHeap.size() == K) {
result = min(result, sumQuality * worker.first);
}
}
return result;
}
int main() {
vector<int> quality = {10, 20, 5};
vector<int> wage = {70, 50, 30};
int K = 2;
double result = minCostToHireWorkers(quality, wage, K);
cout << "Minimum Cost to Hire " << K << " Workers: " << result << endl;
return 0;
}
In this implementation, we first calculate the wage/quality ratio for each worker and store it along with their quality in a pair. We then sort the workers based on their wage/quality ratio. We use a max-heap to keep track of the K
workers with the highest quality. As we iterate through the sorted workers, we update the heap and calculate the total quality of the K
workers. We calculate the cost based on the minimum wage/quality ratio among these K
workers.
The main function demonstrates the usage of minCostToHireWorkers
by calculating the minimum cost to hire K
workers from the given input quality
, wage
, and K
.
The output for the provided test case will be:
Minimum Cost to Hire 2 Workers: 105