leetcode-857-Minimum Cost to Hire K Workers
问题
There are N
workers. The i
-th worker has a quality[i]
and a minimum wage expectation wage[i]
.
Now we want to hire exactly K
workers to form a paid group. When hiring a group of K workers, we must pay them according to the following rules:
- Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.
- Every worker in the paid group must be paid at least their minimum wage expectation.
Return the least amount of money needed to form a paid group satisfying the above conditions.
Example 1:
1 | Input: quality = [10,20,5], wage = [70,50,30], K = 2 |
Example 2:
1 | Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 |
Note:
1 <= K <= N <= 10000
, whereN = quality.length = wage.length
1 <= quality[i] <= 10000
1 <= wage[i] <= 10000
- Answers within
10^-5
of the correct answer will be considered correct.
分析
首先考虑,假设我们现在已经有了K个工人,那么一共应该给他们发多少工资呢?假设每个工人单位工作量所需要的工资为(quality[i]\wage[i])
,那么k个工人所需要支付的总的工资数就是
$$
\sum \limits_{i} quality[i]* \min \limits_{i} \{ quality[i] \div wage[i]\}
$$
那么我们可以按照每个工人的每单位工作量所需要的工资数(quality[i]\wage[i])
对工人进行从小到大的排序。然后依次尝试将每个工人加入到队伍中时,工人所需要支付的总的工资数的情况。若当前队伍人数已经大于等于K时,则从员队伍中取出quality最高的员工,再尝试将新员工加入到队伍中去。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/18/leetcode-857/
License: 知识共享署名-非商业性使用 4.0 国际许可协议