Minimum Cost to Hire K Workers - Problem

There are n workers. You are given two integer arrays quality and wage where:

  • quality[i] is the quality of the i-th worker
  • wage[i] is the minimum wage expectation for the i-th 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.

Input & Output

Example 1 — Basic Case
$ Input: quality = [10,20,5], wage = [70,50,30], k = 2
Output: 105.0
💡 Note: We hire workers at indices 0 and 2. Worker 0 has ratio 70/10=7.0, worker 2 has ratio 30/5=6.0. The captain (highest ratio) is worker 0 with ratio 7.0. Total cost = 7.0 × (10+5) = 105.0
Example 2 — Different k Value
$ Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
Output: 30.666666666666668
💡 Note: We can hire workers at indices 0, 2, 3. Their ratios are 4/3≈1.33, 2/10=0.2, 2/10=0.2. The captain has ratio 4/3. Total cost = (4/3) × (3+10+10) ≈ 30.67
Example 3 — Minimum Case
$ Input: quality = [10,20], wage = [50,80], k = 2
Output: 150.0
💡 Note: We must hire both workers. Worker 0 has ratio 50/10=5.0, worker 1 has ratio 80/20=4.0. Since we need both workers and each must get at least their minimum wage, we use the higher ratio of 5.0. Total cost = 5.0 × (10+20) = 150.0

Constraints

  • n == quality.length == wage.length
  • 1 ≤ k ≤ n ≤ 104
  • 1 ≤ quality[i], wage[i] ≤ 104

Visualization

Tap to expand
Minimum Cost to Hire K Workers INPUT Workers with Quality and Wage W0 Quality: 10 Wage: 70 ratio: 7.0 W1 Quality: 20 Wage: 50 ratio: 2.5 W2 Quality: 5 Wage: 30 ratio: 6.0 Input Arrays quality = [10, 20, 5] wage = [70, 50, 30] k = 2 ALGORITHM STEPS 1 Calculate Ratios ratio = wage/quality 2 Sort by Ratio Ascending order W1(2.5) --> W2(6.0) --> W0(7.0) Sorted by wage/quality ratio 3 Use Max-Heap Track k smallest qualities Max-Heap (qualities): 20 5 sum = 25 4 Calculate Cost cost = sum * max_ratio Try W2: 25 * 6.0 = 150 Best: 15 * 7.0 = 105 FINAL RESULT Optimal Group Selected W2 Quality: 5 Paid: 5 * 7.0 = 35 OK W0 Quality: 10 Paid: 10 * 7.0 = 70 OK Cost Calculation Total Quality = 5 + 10 = 15 Max Ratio = 7.0 (W0) Cost = 15 * 7.0 = 105.0 OUTPUT 105.0 Key Insight: When all workers are paid proportionally to quality with ratio R, the worker with highest wage/quality ratio determines the minimum R needed. By sorting workers by ratio and using a max-heap to track the k smallest qualities, we minimize total cost = (sum of qualities) * (current max ratio). TutorialsPoint - Minimum Cost to Hire K Workers | Greedy with Sorting and Heap
Asked in
Google 35 Amazon 28 Facebook 22 Microsoft 18
58.0K Views
Medium 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