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. oracle删除多个分区表

    declare v_date date; v_part_name varchar(); begin v_date := date'2015-2-4'; while v_date >= date' ...

  2. Python实现SSH传输文件(sftp)

    Windows通过ssh给Linux发送文件 #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import paramiko t ...

  3. How To Install Git on CentOS 7

    Introduction Version control has become an indispensable tool in modern software development. Versio ...

  4. 手机自带输入法emoji表情的输入,提交及显示——前端解决方案

    体验更优排版请移步原文:http://blog.kwin.wang/programming/emoji-transform-commit.html 之前就遇到过需要前端支持用户输入并提交emoji表情 ...

  5. Vulkan Tutorial 04 理解Validation layers

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 What are validation layers? Vulkan API的设计核 ...

  6. jdk环境变量一键设置 管理員运行

    退出360等杀毒软件 本机win10 其他环境自测.参考了网上代码修改. @echo off rem dss color 02 mode con cols=70 lines=30 title JDK ...

  7. 游戏引擎架构Note1

    [游戏引擎架构] 1.第14章介绍的对游戏性相关系统的设计非常有价值.各个开发人员几乎都是凭经验设计,很少见有书籍对这些做总结. 5.通过此书以知悉一些知名游戏作品实际上所采用的方案. 6.书名中的架 ...

  8. WebFlux02 SpringBoot WebFlux项目骨架搭建

    1 环境搭建 1.1 版本说明 jdk -> 1.8 maven -3.5 springboot -> 2.0.3 开发工具 -> IDEA 1.2 创建项目 利用 IDEA 或者 ...

  9. ubuntu opencv的使用

    博客转载自:https://blog.csdn.net/u012816621/article/details/51732932 CMakeLists.txt # cmake needs this li ...

  10. Hyperledger子项目

    Hyperledger由五个子项目构成: • BlockChain Explorer 展⽰和查询区块链块.事务和相关数据的 Web应⽤ • Fabric 区块链技术的⼀个实现(主要项目) • STL ...