# 857. Minimum Cost to Hire K Workers 雇佣 K 名工人的最低成本

## # 题目描述：

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:

``````Input: quality = [10,20,5], wage = [70,50,30], K = 2
Output: 105.00000
Explanation: We pay 70 to 0-th worker and 35 to 2-th worker.
``````

Example 2:

``````Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3
Output: 30.66667
Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.
``````

Note:

1. 1 <= K <= N <= 10000, where N = quality.length = wage.length
2. 1 <= quality[i] <= 10000
3. 1 <= wage[i] <= 10000
4. Answers within 10^-5 of the correct answer will be considered correct.

## # 题目大意

1. K个工人的质量和给他开的工资的比例是相同的。
2. 每个工人都要满足他的最小期望工资。

## # 解题方法

``````class Solution(object):
def mincostToHireWorkers(self, quality, wage, K):
"""
:type quality: List[int]
:type wage: List[int]
:type K: int
:rtype: float
"""
works = sorted([(w / float(q), q) for w, q in zip(wage, quality)])
que = []
qsum = 0
res = float("inf")
for rate, q in works:
heapq.heappush(que, -q)
qsum += q
if len(que) > K:
qsum += heapq.heappop(que)
if len(que) == K:
res = min(res, qsum * rate)
return res
``````

https://leetcode.com/problems/minimum-cost-to-hire-k-workers/discuss/141768/Detailed-explanation-O(NlogN)

## # 日期

2018 年 10 月 5 日 —— 转眼假期要结束了！！