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.

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的更多相关文章

  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. 【LeetCode】857. Minimum Cost to Hire K Workers 解题报告(Python)

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

  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. poj-2516.minimum cost(k次费用流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19883   Accepted: 7055 Des ...

  7. Minimum Cost(最小费用最大流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  8. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  9. [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 ...

随机推荐

  1. mysql安装及基本操作(mysql作业)

    1 官网下载,链接  https://www.mysql.com/downloads/ Download MySQL Community Server 默认为你选好了Mac OS X 平台 选择的是. ...

  2. Java学习之App开发公司手机端设想

    背景:最近在学JAVA,看到JAVA做各种APP,而公司软件主要是做家居设计,使用者多是设计师和家具门店,很难让大部分非专业人士接触到我们的产品,由于设计复杂且占用资源较多不太可能用APP实现网站设计 ...

  3. Oray.com花生壳路由器配置注意

    当路由器不链接wan口,只链接lan口时,此路由器其实就是当做一个无线交换机使用了,在此种情况下,花生壳登录会失败,因为花生壳本身也认为这设备不是路由器.

  4. Python Twisted系列教程8:使用Deferred的诗歌下载客户端

    作者:dave@http://krondo.com/deferred-poetry/  译者:杨晓伟(采用意译) 可以从这里从头开始阅读这个系列. 客户端4.0 我们已经对deferreds有些理解了 ...

  5. Python Twisted系列教程7:小插曲,Deferred

    作者:dave@http://krondo.com/an-interlude-deferred/  译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列 回调函数的后序发展 在第六部分我们认识这 ...

  6. JDBC概述及连接数据库

    一.JDBC简介:  JDBC(Java Data Connectivity,java数据库连接)是一种用于执行sql语句的JavaAPI,可以为多种关系数据库提供统一访问,它由一组用Java语言编写 ...

  7. delphi OleVariant转换RecordSet

    delphi OleVariant转换RecordSet uses Data.Win.ADODB; function varToRecordSet( parms : OleVariant ) : Da ...

  8. MySQL OSC(在线更改表结构)原理

    1 OSC介绍 在我们的数据库操作中,更改表结构是一个常见的操作,而当我们的表数据量非常大时,我们更改表结构的时间是非 常的长,并且在跟改期间,会生成一个互斥锁,阻塞对整个表的所有操作,这样,对于我们 ...

  9. Unity3D自带Demo AngryBots路径

    [Unity3D自带Demo AngryBots路径] 1.Windows: C:\Users\Public\Documents\Unity Porjects 2.MacOSX: /Users/Sha ...

  10. java Web jsp四大作用域和九大内置对象

    JSP中的四大作用域:page.request.session.application 这四大作用域,其实就是其九大内置对象中的四个,为什么说他们也是JSP的四大作用域呢?因为这四个对象都能存储数据, ...