作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/minimum-cost-to-hire-k-workers/description/

题目描述:

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.

题目大意

雇工人的题目。每个工人都有自己的质量和一个最小的期望工资。现在要找出K个工人,要求:

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

最后求雇佣K个工人,需要支付的最小钱数。

解题方法

这个题不是很好理解,经过我的仔细推敲后,觉得可以这么表述:每个工人都有自己期望的价性比,雇佣K个工人的时候要满足每个人的实际价性比不低于他的期望,即需要按照K个工人中的最高期望价性比给这K个人开工资。问需要的最少的工资是多少。注意使用的是价性比,不是性价比。因为性价比是我们买东西的时候希望的,而这些工人是出卖自己的劳动力的,因此他们希望得到更高的价性比。而作为选择工人的这一方,我们希望工人的性价比更高点,但是啊得注意,性价比高的工人会被那些性价比低的工人抬高工资,即他也会要求更高的工资,没有工人愿意看到别人好吃懒做还拿高工资,所以性价比高的工人会索要更高的工资,导致自己性价比和好吃懒做的工人一样。

如果理解了上面的那段话,那么我们需要按照价性比来做排序,然后依次遍历,得到K个工人的工资总和。

使用了一个大根堆,来获取K个工人的最大的价性比,作为K个工人的价性比,使用qsum保存K个工人的质量和。要给他们付的工资就是qsum * 最大性价比。

时间复杂度是O(Nlog(N)),空间复杂度是O(N)。

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 日 —— 转眼假期要结束了!!

【LeetCode】857. Minimum Cost to Hire K Workers 解题报告(Python)的更多相关文章

  1. [LeetCode] 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 w ...

  2. [LeetCode] 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 w ...

  3. 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 w ...

  4. [Swift]LeetCode857. 雇佣 K 名工人的最低成本 | 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 w ...

  5. 雇佣K个工人的最小费用 Minimum Cost to Hire K Workers

    2018-10-06 20:17:30 问题描述: 问题求解: 问题规模是10000,已经基本说明是O(nlogn)复杂度的算法,这个复杂度最常见的就是排序算法了,本题确实是使用排序算法来进行进行求解 ...

  6. 【LeetCode】787. Cheapest Flights Within K Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 参考资料 日期 题目 ...

  7. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  8. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

  9. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

随机推荐

  1. account, accomplish, accumulate

    account account从词源和count(数数)有关,和computer也有点关系.calculate则和'stone used in counting'有关.先看两个汉语的例子:1. 回头再 ...

  2. 源码分析-Producer

    消息生产者的代码都在client模块中,相对于RocketMQ来讲,消息生产者就是客户端,也是消息的提供者. 方法和属性 主要方法介绍 //创建主题 void createTopic(final St ...

  3. Shell学习(二)——变量和基本数据类型

    参考博客: [1]LinuxShell脚本--变量和数据类型 [2]shell只读变量删除 一.变量 定义变量的语法 定义变量时,变量名和变量值之间使用"="分隔,并且等号两边不能 ...

  4. Running shell commands by C++

    #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; st ...

  5. C++ 类型转换(C风格的强制转换):

    转https://www.cnblogs.com/Allen-rg/p/6999360.html C++ 类型转换(C风格的强制转换): 在C++基本的数据类型中,可以分为四类:整型,浮点型,字符型, ...

  6. SQL优化原理

    SQL优化过程: 1,捕获高负荷的SQL语句-->2得到SQL语句的执行计划和统计信息--->3分析SQL语句的执行计划和统计信息--->4采取措施,对SQL语句进行调整.1找出高负 ...

  7. 阿里云esc 安装 docker

    1. 更新 yum 到最新: yum update (用 root 用户登录,无需加 sudo,如果不是,需要加,即  yum update ) 2. 安装软件包:yum-util(提供 yum-co ...

  8. apply 和 call 的区别

    相同点: 都能够改变方法的执行上下文(执行环境),将一个对象的方法交给另一个对象来执行,并且是立即执行 不同点: call方法从第二个参数开始可以接收任意个参数,每个参数会映射到相应位置的func的参 ...

  9. spring整合mybatis和springmvc的配置概图

  10. 关于finally中的语句和try中的return之间的执行顺序

    首先是第一种情况: 我们这里由于程序只是单一的,所以后面的代码只有主题部分: Public class test{ Public static void main(String[] args){ Sy ...