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:

  1. Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.
  2. 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.

这道题说是有N个员工,每个员工有个能力值,还有个薪水期望值,现在我们需要从中雇佣K个员工,需要满足两个条件:1. 每个员工的薪水要和其能力值成恒定比例。2. 每个员工的薪水不低于其期望值。让求雇佣满足要求的K个员工的最小花费是多少,博主最开始看到这题时,以为是背包问题,又看了一下是 Hard 的难度,更加坚定了是个 DP 问题。但实际上这却是一道贪婪算法能解决的问题,类型标签给的是堆 Heap,嗯,因缺斯汀。我们仔细来分析分析题目中限定必须满足的两个条件,先来看第一个,说是每个员工的薪水要和其能力值成恒定比例,意思是说假如两个员工A和B,若A的能力值是B的2倍,那么A的薪水就要是B的两倍,要雇佣的K个员工所有人的薪水和能力都是要成比例的,而这个比例一定是个恒定值,只要能够算出这个最低的薪水能力比例值,乘以K个员工的总能力值,就可以得到最少的总花费。第二个需要满足的条件是每个员工的薪水不能低于其期望值,则每个员工都有一个自己固定的薪水能力比例值,而需要求的那个最低的薪水能力比例值不能小于任何一个员工自己的比例值。当员工能力值越低,期望薪水越高的时候,其薪水能力比例值就越大,所以可以根据薪水能力比例值从大到小来排列员工。可以将员工的薪水能力比例值和其能力值组成 pair 对儿放到一个数组中,然后对这个数组进行排序,则默认就是对薪水能力比例值进行从小到大的排列。接下来的操作就跟之前那道 [Top K Frequent Words](http://www.cnblogs.com/grandyang/p/7689927.html) 非常像了,这里用一个最大堆,还要用一个变量 qsum 来累加员工的能力值,先将薪水能力比例最低的员工的能力值加到 qsum 中,同时加入到最大堆中,若堆中员工总数大于K了,则将堆顶能力值最大的员工移除,因为能力值越大意味着需要付的薪水越多。若堆中员工总数等于K了,则用当前员工的薪水能力比例乘以总的能力值数得到一个总花费,用来更新结果 res。为啥这样是正确的呢?因为当前员工的薪水能力比例值是大于堆中其他所有员工的,那么乘以恒定的总能力值,得出的总薪水数一定大于等于使用其他员工的薪水能力比例值,则每个员工可得到的薪水一定是大于等于其期望值的,这样就同时满足了两个条件,所以是符合题意的,最终更新完得到的总花费一定是最低的,参见代码如下:

class Solution {
public:
double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int K) {
double res = DBL_MAX, qsum = 0, n = quality.size();
vector<pair<double, int>> workers;
priority_queue<int> pq;
for (int i = 0; i < n; ++i) {
workers.push_back({double(wage[i]) / quality[i], quality[i]});
}
sort(workers.begin(), workers.end());
for (auto worker : workers) {
qsum += worker.second;
pq.push(worker.second);
if (pq.size() > K) {
qsum -= pq.top(); pq.pop();
}
if (pq.size() == K) {
res = min(res, qsum * worker.first);
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/857

类似题目:

Top K Frequent Words

参考资料:

https://leetcode.com/problems/minimum-cost-to-hire-k-workers/

https://leetcode.com/problems/minimum-cost-to-hire-k-workers/discuss/185085/75ms-Java-with-Explanations

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

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 857. Minimum Cost to Hire K Workers 雇佣K名工人的最低成本的更多相关文章

  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 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  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. 贪心-谷歌-857. 雇佣 K 名工人的最低成本

    2020-03-15 22:00:39 问题描述: 有 N 名工人. 第 i 名工人的工作质量为 quality[i] ,其最低期望工资为 wage[i] . 现在我们想雇佣 K 名工人组成一个工资组 ...

  6. leetcode_雇佣 K 名工人的最低成本(优先级队列,堆排序)

    题干: 有 N 名工人. 第 i 名工人的工作质量为 quality[i] ,其最低期望工资为 wage[i] . 现在我们想雇佣 K 名工人组成一个工资组.在雇佣 一组 K 名工人时,我们必须按照下 ...

  7. LeetCode 1000. Minimum Cost to Merge Stones

    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...

  8. LeetCode 1130. Minimum Cost Tree From Leaf Values

    原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...

  9. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

随机推荐

  1. MySchool题目

    题目: 1.查询所有学生记录,包含年级名称2.查询S1年级下的学生记录 一.项目目录 二.com.myschool.dao 2.1 BaseDao package com.myschool.dao; ...

  2. Debug 路漫漫-15:Python: NameError:name 'dataset' is not defined

    在调试 <Outer Product-based Neural Collaborative Filtering>论文的源码(https://github.com/duxy-me/ConvN ...

  3. Allure自动化测试报告我是这样用的

    关于自动化测试报告: 之前用过testNG自带的测试报告.优化过reportNG的测试报告.extentreport.Zreport(大飞总原创),这些是我之前都用过的,也是在去年雯姐和我说过Allu ...

  4. Pytest 使用简介

    前言 最近在听极客时间的课程,里面的讲师极力推崇 pytest 框架,鄙视 unittest 框架,哈哈!然后查了些资料,发现了一条 python 鄙视链:pytest 鄙视 > unittes ...

  5. 【Oracle】Oracle自动内存管理AMM

    Oracle自动内存管理AMM AMM(Automatic Memory Management)自动内存管理,分配一整块内存区域,Oracle数据库自动分配管理SGA和PGA的内存.具体通过设置两个参 ...

  6. Vue.js 源码分析(四) 基础篇 响应式原理 data属性

    官网对data属性的介绍如下: 意思就是:data保存着Vue实例里用到的数据,Vue会修改data里的每个属性的访问控制器属性,当访问每个属性时会访问对应的get方法,修改属性时会执行对应的set方 ...

  7. hdu-6071 Lazy Running

    In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule ...

  8. python asyncio 使用ThreadPoolExecutor和asyncio完成阻塞IO请求

    #使用多线程:在协程中集成阻塞io import asyncio from concurrent.futures import ThreadPoolExecutor import socket fro ...

  9. python asyncio call_soon, call_at, call_later

    1. call_soon, 协程一运行就马上运行 def callback(sleep_times): print("success time {}".format(sleep_t ...

  10. [KMP]一本通(http://ybt.ssoier.cn:8088) 1698:字符串匹配

    字符串匹配 [题目描述] 对于一个字符集大小为C的字符串pp,可以将任意两个字符在p中的位置进行互换,例如p=12321,交换1.21.2得到21312,交换1.4得到42324,交换可以进行任意次. ...