Problem

There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the ith worker and wage[i] is the minimum wage expectation for the ith worker.

We want to hire exactly k workers to form a paid group. To hire a group of k workers, we must pay them according to the following rules:

  1. Every worker in the paid group must be paid at least their minimum wage expectation.
  2. In the group, each worker’s pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.

Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within $10^{-5}$ of the actual answer will be accepted.

Examples

Example 1:

Input: quality = [10,20,5], wage = [70,50,30], k = 2
Output: 105.00000
Explanation: We pay 70 to 0th worker and 35 to 2nd worker.

Example 2:

Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
Output: 30.66667
Explanation: We pay 4 to 0th worker, 13.33333 to 2nd and 3rd workers separately.

Solution

Method 1 - Using quality to wage ratio and Priority Queue

This problem can also be thought of as if we are going in market, and buying different baskets of bananas OR apples. Now, the example looks like:

Now, the rule is, when we buy k baskets, we have to pay the highest unit price for all bananas.

For e.g. - if we buy 20 bananas of ₹2.5/unit and 10 bananas of ₹7/unit together, we have to pay the highest ₹7/unit for all of them (20 + 10) * ₹7/unit

We want k baskets, we try the cheapest bananas first i.e. cheapest in terms of unit price.

  1. So, we start with ₹2.5/unit (buy 20 units) ⇨ It costed us 50.
  2. Add another basket, try the next higher unit price is ₹6/unit (buy 5 units). Now all bananas are ₹6/unit (20 + 5) ⇨ It costed us ₹150. Now, we have touched target of k baskets. But, we still need to process more baskets, so which basket should we remove from selection?
  3. The next unit price to try will be higher ₹7/unit.
    1. If we need to remove one basket, they have the same unit price, we remove the ones with more units or quantity i.e. the 20 one. Now, we have ₹6/unit (for 5 unit).
    2. Now, all bananas are at ₹7/unit (10 + 5) ⇨ Now, the cost is 105

Cheaper unit price will never be used again, so removing the baskets with highest number of bananas i.e. quantity is always safe.

Algorithm

We have 2 requirements:

  1. Every worker in the paid group must be paid at least their minimum wage expectation.
  2. In the group, each worker’s pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.

Now, lets jump to algorithm

  1. Sort the input arrays based on the unit price aka $W_i / Q_i$, where $W_i$ is wage of ith worker and $Q_i$ is quality. (We sort because to suite requirement 2). Lets call it unit price array.
  2. Now, we use maxHeap of size k to select the workers in group. So, we start iterating on these unit prices.
    1. Each time, we add worker to group, we increment totalQuality.
    2. Now, when the heap size is k, we have formed first group and. we calculate answer. But that also increases totalCost. Total cost is totalQuality * currWorkerUnitPrice. As, the unit prices are in increasing order, we increase each worker’s unit price. This way we also fulfil requirement 1.
    3. Now, the new worker comes in. This time we repeat step 1, but our heap size will be more than k. So, we remove the worker with most quality from heap, and continue till we process all the workers.
  3. Return the minimum totalCost.

Video Explanation

Here is the video explanation:

Code

Java
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
	int n = quality.length;
	List<Integer> workers = new ArrayList<>(n);
	for(int i = 0; i < n; i++) {
		workers.add(i);
	}
	Collections.sort(workers, (a,b) -> Double.compare(wage[a] / (double) quality[a], wage[b] / (double) quality[b]));
	
	PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> Integer.compare(quality[b], quality[a]));

	double totalPrice = Double.MAX_VALUE;
	int totalQuality = 0;

	for (int workerIdx: workers) {
		totalQuality += quality[workerIdx];
		maxHeap.add(workerIdx);

		if (maxHeap.size() > k) {
			totalQuality -= quality[maxHeap.remove()];
		}

		if (maxHeap.size() == k) {
			double unitPrice = wage[workerIdx] / (double) quality[workerIdx];
			totalPrice = Math.min(totalPrice, totalQuality * unitPrice);
		}
	}
	return totalPrice;
}

Complexity

  • ⏰ Time complexity: O(n log n) - We use O(n log n) for sorting the array, and O(nlogk) when iterating on n workers and adding and removing them from maxHeap.
  • 🧺 Space complexity: O(n) - We use O(n) for storing values in array, and O(k) for heap.