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 workerwage[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:
- Every worker in the paid group must be paid at least their minimum wage expectation
- 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code