857. Minimum Cost to Hire K Workers
Asked at Apple
Difficulty
Hard
Time
O(n log n)
Space
O(n)
Pattern
Array
Companies
1
Problem
You are given two integer arrays, quality and wage, both of length n. Worker i has quality quality[i] and expects to be paid at least wage[i]. You must hire exactly k workers.
Two rules apply to the group you hire. First, every hired worker must get at least their own wage[i]. Second, pay must be proportional to quality: if one worker's quality is twice another's, their pay must be exactly twice as much. So once you fix a pay rate r (dollars per unit of quality), every hired worker i earns r * quality[i].
Return the smallest total cost of a valid group of k workers. A group is valid if there is some rate r such that r * quality[i] >= wage[i] for every hired worker. Answers within 10^-5 of the true value are accepted.
One rate for the whole group
Constraints
- ●
n == quality.length == wage.length - ●
1 <= k <= n <= 10^4 - ●
1 <= quality[i], wage[i] <= 10^4
Examples
quality = [10,20,5], wage = [70,50,30], k = 2105.00000quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 330.66667Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Apple | Hard | View all Apple questions → |
How to Think About It
Compute the wage-to-quality ratio for each worker and sort by this ratio
Iterate through workers sorted by ratio, treating each as the ratio-defining worker
Maintain a max-heap of K-1 qualities from previously seen workers
For each worker, the cost is the sum of K-1 largest qualities times the current ratio
Track the minimum cost across all possible ratio-defining workers
Optimal Approach
Sort workers by their wage-to-quality ratio. For each worker i (in sorted order), assume worker i defines the group ratio (all workers are paid at quality[i] * ratio_i). To minimize cost with this ratio, pick K-1 workers from among those with ratio <= ratio_i who have the smallest qualities. Use a max-heap to maintain the K-1 smallest qualities. When the heap reaches size K-1, the cost is (sum of heap + quality[i]) * ratio_i. Track the minimum cost. The key insight is that the highest-ratio worker in any group determines the pay rate.
Sort by ratio, keep the k smallest qualities
What Trips People Up in Real Interviews
Clarify that pay is proportional: paid[i] = quality[i] * ratio
Explain that the worker with the highest ratio determines the group ratio
Mention why sorting by ratio and using a max-heap gives the optimal solution
Discuss that the heap maintains the K-1 workers with smallest quality from previous groups
State the time complexity is O(N log N) for sorting and O(N log K) for heap operations
Solution Code
O(n log n) timeimport heapq
class Solution:
def mincostToHireWorkers(self, quality: list[int], wage: list[int], k: int) -> float:
workers = sorted([(w / q, q) for q, w in zip(quality, wage)])
result = float('inf')
total_quality = 0
max_heap = []
for ratio, q in workers:
total_quality += q
heapq.heappush(max_heap, -q)
if len(max_heap) > k:
total_quality += heapq.heappop(max_heap)
if len(max_heap) == k:
result = min(result, total_quality * ratio)
return resultFrequently Asked Questions
What is the Minimum Cost to Hire K Workers problem?
Given N workers with quality and minimum wage expectations, hire exactly K workers such that the total cost is minimized. Each hired worker must be paid proportionally based on their quality-to-wage ratio. This problem combines greedy sorting with heap-based optimization.
How do you solve Minimum Cost to Hire K Workers?
The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Minimum Cost to Hire K Workers?
Minimum Cost to Hire K Workers is asked at Apple. It is a hard difficulty problem.
What are common mistakes on Minimum Cost to Hire K Workers?
- Clarify that pay is proportional: paid[i] = quality[i] * ratio
- Explain that the worker with the highest ratio determines the group ratio
- Mention why sorting by ratio and using a max-heap gives the optimal solution
- Discuss that the heap maintains the K-1 workers with smallest quality from previous groups
- State the time complexity is O(N log N) for sorting and O(N log K) for heap operations