Minimum Cost to Hire K Workers
Minimum Cost to Hire K Workers is LeetCode 857 (Hard). Worker i has a quality[i] and a minimum wage wage[i]. You must hire exactly k workers, and their pay must follow two rules:
- Everyone in the group is paid the same amount per unit of quality, so pay is proportional to quality.
- Nobody is paid less than their minimum wage.
Return the least total pay for a group of k workers. Answers within 10⁻⁵ of the true value are accepted. With up to 10,000 workers, trying every group of k is out of the question.
- n == quality.length == wage.length
- 1 <= k <= n <= 10⁴
- 1 <= quality[i], wage[i] <= 10⁴
Intuition
Give each worker a rate: wage / quality, the pay per unit of quality they insist on. A group's shared rate has to satisfy its most demanding member, so it is the group's highest rate, and the bill is highest rate × total quality.
Now fix which worker sets the rate. Sort workers by rate. If worker i has the highest rate in the group, the other k - 1 members come from the workers before i in that order, and the cheapest choice is the ones with the smallest quality. A max-heap keeps the k smallest qualities seen so far: add each new worker, and when the heap holds too many, remove the largest quality.
"Choose k items; the cost is one item's maximum of something times the sum of something else" is sort-by-the-first, heap-on-the-second. Maximum Performance of a Team (LeetCode 1383) and Maximum Subsequence Score (LeetCode 2542) use the same pattern with a minimum instead of a maximum.
Approach
Before reading on, work out what one group of k workers costs, in terms of their rates and qualities. Then ask: if you knew which worker sets the rate, which other workers would you pick? Aim for O(n log n).
Sort workers by rate
Pair each quality with its wage and sort by wage / quality. Walking in this order, the worker just added always has the highest rate seen so far, so that worker sets the pay for any group drawn from the workers seen.
Add the worker's quality to the group
Push the quality onto a max-heap and add it to total. The heap holds the candidate group, and total is the group's combined quality.
Remove the largest quality when the group is too big
If the heap now holds k + 1 workers, pop the largest quality and subtract it from total. Removing the largest quality lowers the bill the most, and the remaining workers' rates are still covered.
Price the group
Once the heap holds exactly k workers, the cost is total × rate of the current worker. Keep the smallest cost seen; after the last worker it is the answer.
Code in Python, C++ and Java
Common pitfalls
Dividing with integers
best = Math.min(best, total * wage[i] / quality[i]); // all ints
best = Math.min(best, total * ((double) wage[i] / quality[i]));
Integer division throws away the fraction. On quality [3,1,10,10,1], wage [4,8,2,2,7], k = 3 the answer is 30.66667, but the integer version returns 30. In Python, / is already true division; // would make the same mistake.
Removing the smallest quality instead of the largest
heapq.heappush(heap, q) ... total -= heapq.heappop(heap)
heapq.heappush(heap, -q) ... largest = -heapq.heappop(heap) total -= largest
Python's heapq is a min-heap, so without the minus sign the pop removes the cheapest worker and keeps the expensive ones. On quality [10,20,5], wage [70,50,30], k = 2 this returns 150 instead of 105.
Edge cases
One worker is paid exactly their minimum wage, so the answer is the smallest wage. The loop gets there because the heap always keeps the single smallest quality, priced at a rate that covers it.
Everyone is hired, so the heap first reaches size k at the last worker. That one price, total quality × highest rate, is the answer.
Complexity
Minimum Cost to Hire K Workers FAQ
What if the worker just added is the one that gets removed?
Then the heap no longer contains the worker whose rate is used, so that price is too high. It does no harm: the same k workers were in the heap one step earlier, priced at a lower or equal rate, so a cheaper price for this exact group has already been counted.
Why not sort by quality or by wage alone?
Neither tells you who sets the pay rate. A worker with a small wage can still have a very high rate if their quality is tiny, and including them would raise every other member's pay.