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:
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 <= 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.
Approach #1: C++.
class Solution {
public:
double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int K) {
vector<vector<double>> workers;
for (int i = 0; i < wage.size(); ++i)
workers.push_back({(double)wage[i]/quality[i], (double)quality[i]}); sort(workers.begin(), workers.end()); double res = INT_MAX, qsum = 0; priority_queue<int> pq;
for (auto worker : workers) {
qsum += worker[1];
pq.push(worker[1]);
if (pq.size() > K) qsum -= pq.top(), pq.pop();
if (pq.size() == K) res = min(res, qsum*worker[0]);
}
return res;
}
};
Analysis:
In this solution we use a vector to store the ratio of wage/quality and the quality, then sort the vector with ratio.
We travel the vector when the priority_queue's size < k we add the quality to the qsum.
When priority_queue's size == K we calculate the total wages at this status.
Last we select the minimum total wages as the result.
Time Complexity
O(NlogN)
for sort.O(NlogK)
for priority queue.
Approach #2: Java. [Greedy]
class Solution {
public double mincostToHireWorkers(int[] quality, int[] wage, int K) {
int N = quality.length;
double ans = 1e9; for (int captain = 0; captain < N; ++captain) {
double factor = (double)wage[captain] / quality[captain];
double prices[] = new double[N];
int t = 0;
for (int worker = 0; worker < N; ++worker) {
double price = factor * quality[worker];
if (price < wage[worker]) continue;
prices[t++] = price;
} if (t < K) continue;
Arrays.sort(prices, 0, t);
double cand = 0;
for (int i = 0; i < K; ++i)
cand += prices[i];
ans = Math.min(ans, cand);
} return ans;
}
}
Analysis:
Having the similar thinking with above code, but this solution don't use heap to maintain the ratio, so the time complex is bigger than above.
Time Complexity:
O(N^2 \log N)O(N2logN), where NN is the number of workers.
857. Minimum Cost to Hire K Workers的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】857. Minimum Cost to Hire K Workers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- [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 ...
- 雇佣K个工人的最小费用 Minimum Cost to Hire K Workers
2018-10-06 20:17:30 问题描述: 问题求解: 问题规模是10000,已经基本说明是O(nlogn)复杂度的算法,这个复杂度最常见的就是排序算法了,本题确实是使用排序算法来进行进行求解 ...
- poj-2516.minimum cost(k次费用流)
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19883 Accepted: 7055 Des ...
- Minimum Cost(最小费用最大流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
随机推荐
- 在液晶屏里显示浮点数的方法 (sprintf 的妙用)
思路:使用 sprintf 函数将浮点型数据转为指定格式的字符串 #include <stdio.h> #include<string.h> int main() { unsi ...
- 根文件系统的构建与分析(四)之瑞士军刀busybox生成系统基本命令
根文件系统的构建与分析(四) 转载请注明 http://blog.csdn.net/jianchi88 Author:Lotte 邮箱:baihaowen08@126.com ls /bin, ...
- SDRAM 之时序收敛(学习了特权老师)
到现在我还是不太理解SDRAM的时序设置,但是可能蒙对了.(呵呵) 开发环境: quartus II 13.0 板子: DE2 EP2C35F672C6N 时序约束step 1:create cl ...
- VS加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS
网上找的几个方法都不行 最后自己解决了.首先打开该项目得csproj文件,找到<ProjectExtensions>这个标签,是在最后部分,然后把<UseIIS>True< ...
- IDA Pro 权威指南学习笔记(二) - IDA 数据库文件
生成数据库文件 把要分析的文件用 IDA 打开后,会生成 3 个数据库文件 扩展名分别为 .id0,id1,nam .id0 文件是一个二叉树形式的数据库 .id1 文件包含描述每个程序字节的标记 . ...
- krpano之语音介绍
语音介绍:在每进入一个场景时,播放一段该场景的语音介绍. 制作步骤: 1.定义全局事件.在关闭场景时执行stopsounds(),在打开新场景时执行automusic(). <events on ...
- MYSQL中str_to_date函数的用法
str_to_date(str,format) 函数的用法 str_to_date函数将str转化为日期型的数据,format表示转化后的格式. format参数格式: 常用: %Y 年 %m 月 ...
- PHP数据结构之三 线性表中的单链表的PHP实现
线性表的链式存储:用一组任意的存储单元存储线性表中的数据元素.用这种方法存储的线性表简称线性链表. 链式存储线性表的特点:存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是零散分 ...
- pl/sql简介
- NODEJS网站
nodejs https://nodejs.org/en/ nodejs官网 http://nodeapi.ucdok.com/#/api/ nodejs手册 https://www.npmjs.co ...