Minimum Cost to Hire K Workers - Problem

You're running a company and need to hire exactly k workers from a pool of n candidates. Each worker has a quality rating and a minimum wage expectation.

Here's the challenge: You must follow these strict hiring rules:

  • ๐Ÿ“‹ Rule 1: Every hired worker must receive at least their minimum wage expectation
  • โš–๏ธ Rule 2: All workers' pay must be proportional to their quality - if Worker A has twice the quality of Worker B, they must be paid exactly twice as much

Given arrays quality[i] and wage[i] representing each worker's quality and minimum wage expectation, find the minimum total cost to hire exactly k workers while satisfying both rules.

Example: If you hire workers with qualities [3, 1, 2] and they need wages [4, 8, 2], you might pay them [12, 4, 8] respectively (proportional to quality 3:1:2) while meeting everyone's minimum wage.

Input & Output

example_1.py โ€” Basic Case
$ Input: quality = [10,20,5], wage = [70,50,30], k = 2
โ€บ Output: 105.00000
๐Ÿ’ก Note: We hire workers 0 and 2. Worker 0: ratio = 70/10 = 7.0, Worker 2: ratio = 30/5 = 6.0. The max ratio is 7.0, so we pay 7.0 ร— (10+5) = 105.0 total.
example_2.py โ€” Equal Ratios
$ Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
โ€บ Output: 30.66667
๐Ÿ’ก Note: We hire workers with qualities [3,1,10] and ratios [4/3, 8/1, 2/10]. The max ratio is 8.0, so total cost is 8.0 ร— (3+1+10)/3 = 30.67.
example_3.py โ€” Edge Case k=1
$ Input: quality = [2,3,5], wage = [10,15,20], k = 1
โ€บ Output: 10.00000
๐Ÿ’ก Note: When k=1, we simply choose the worker with minimum wage since there's no proportional constraint with other workers. Worker 0 costs 10.

Constraints

  • n == quality.length == wage.length
  • 1 โ‰ค k โ‰ค n โ‰ค 104
  • 1 โ‰ค quality[i], wage[i] โ‰ค 104
  • All workers must be paid proportionally to their quality

Visualization

Tap to expand
Minimum Cost Worker Hiring StrategyAvailable Workers (Quality, Wage)10,$7020,$505,$3015,$458,$40Step 1: Calculate wage/quality ratiosWorker 1Ratio: 7.0Worker 2Ratio: 2.5Worker 3Ratio: 6.0Step 2: Sort by ratio and find optimal groupSorted by ratio (ascending)Worker 2Ratio: 2.5Quality: 20Worker 3Ratio: 6.0Quality: 5Worker 1 (not selected)Final Calculation (k=2)Cost = Max Ratio ร— Total Quality = 6.0 ร— (20+5) = 150.0๐ŸŽฏ Choose workers with lowest ratios to minimize the maximum ratio in the group
Understanding the Visualization
1
Calculate Ratios
For each worker, calculate their wage-to-quality ratio (their 'price per unit of work')
2
Sort by Efficiency
Sort workers by their ratio - those with lower ratios are more 'cost-efficient'
3
Sliding Window
Process workers in order, maintaining a group of k workers with lowest total quality
4
Heap Optimization
Use max heap to efficiently track and remove highest quality workers when needed
5
Cost Calculation
For each valid group, calculate cost as max_ratio ร— total_quality
Key Takeaway
๐ŸŽฏ Key Insight: The worker with the highest wage-to-quality ratio in any hired group determines everyone's pay rate. By processing workers in order of increasing ratio and using a heap to maintain the k lowest-quality workers, we efficiently find the optimal hiring combination.
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28 Apple 22
35.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen