首页 > 其他分享 >LeetCode 857. Minimum Cost to Hire K Workers

LeetCode 857. Minimum Cost to Hire K Workers

时间:2024-07-02 11:10:56浏览次数:16  
标签:wage Hire 857 group double Workers maxHeap worker quality

原题链接在这里:https://leetcode.com/problems/minimum-cost-to-hire-k-workers/description/

题目:

There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the ith worker and wage[i] is the minimum wage expectation for the ith worker.

We want to hire exactly k workers to form a paid group. To hire a group of k workers, we must pay them according to the following rules:

  1. Every worker in the paid group must be paid at least their minimum wage expectation.
  2. In the group, each worker's pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.

Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 10-5 of the actual answer will be accepted.

Example 1:

Input: quality = [10,20,5], wage = [70,50,30], k = 2
Output: 105.00000
Explanation: We pay 70 to 0th worker and 35 to 2nd 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 0th worker, 13.33333 to 2nd and 3rd workers separately. 

Constraints:

  • n == quality.length == wage.length
  • 1 <= k <= n <= 104
  • 1 <= quality[i], wage[i] <= 104

题解:

There are two rules. one is the pay the minimum wage expectation. the second is to pay the same ratio for the group.

Thus we want to have a relatively smaller ratio.

We first sort based on ratio. The kth ratio is a starting candidate for total cost.
But quality may be to high, thus we want to try the next candidate, ratio may be higher, but total quality may be lower.

To exclude the highest quality, we maintain a maxHeap.

Time Complexity: O(nlogn). n = quality.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
 3         int n = quality.length;
 4         double[][] worker = new double[n][2];
 5         for(int i = 0; i < n; i++){
 6             worker[i] = new double[]{(double)wage[i] / quality[i], quality[i]};
 7         }
 8 
 9         Arrays.sort(worker, (a, b) -> Double.compare(a[0], b[0]));
10         double res = Double.MAX_VALUE;
11         PriorityQueue<Double> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
12         double countSum = 0;
13         for(double[] w : worker){
14             countSum += w[1];
15             maxHeap.add(w[1]);
16             if(maxHeap.size() > k){
17                 countSum -= maxHeap.poll();
18             }
19 
20             if(maxHeap.size() == k){
21                 res = Math.min(res, countSum * w[0]);
22             }
23         }
24 
25         return res;
26     }
27 }

 

标签:wage,Hire,857,group,double,Workers,maxHeap,worker,quality
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18279504

相关文章

  • 基于 Cloudflare Workers 和 cloudflare-docker-proxy 搭建镜像加速服务
    本文主要介绍了如何基于CloudflareWorkers和cloudflare-docker-proxy搭建dockerhub、gcr、quay等镜像加速服务。最近,受限于各种情况,部分主流镜像站都关了,为了能够正常使用,建议自己搭建一个加速器。写文之前,也已经部署好了一个,可以直接使用,具体使用方法跳转https://docke......
  • Springboot计算机等级考试在线答题小程序 毕业设计-附源码68573
    摘 要计算机等级考试在线答题小程序主要功能模块包括用户管理、考试动态、考试须知、在线考试、用户反馈等,采取面对对象的开发模式进行软件的开发和硬体的架设,能很好的满足实际使用的需求,完善了对应的软体架设以及程序编码的工作,采取Mysql作为后台数据的主要存储单元,采用Sp......
  • The field file exceeds its maximum permitted size of 1048576 bytes
    问题—基于Springboot项目,文件上传功能报错Causedby:Thefieldfileexceedsitsmaximumpermittedsizeof1048576bytes.文件的大小超出了允许的范围。错误原因SpringBoot内嵌的Tomcat默认的所有上传的文件大小为1MB,超出这个大小就会报错,解决这个问题需要更改以下......
  • 基于cloudflare workers自建docker镜像
    缘由因为近期国内镜像站点的变动,自建docker镜像也提上了日程。顺便发现了Hammal这个优秀的项目。Hammal是运行于cloudflareworkers上的Docker镜像加速工具,用于解决获取Docker官方镜像速度缓慢以及完全无法获取k8s.gcr.io上镜像的问题。在这里感谢如下两个项目tomwei......
  • 「杂题乱刷」P8572
    链接发现这东西就很根号分治。考虑两种情况:\(k\le1000\),这部分直接用前缀和维护然后暴力做,时间复杂度\(O(kq)\)。\(k>1000\),此时\(n\le500\),这部分直接预处理答案,时间复杂度\(O(n^2k)\)。两个时间复杂度均正确,因此可以通过此题。代码:点击查看代码/*Tips......
  • vue3 引入workers 大量优化业务代码和复杂的计算的代码
    前沿vite页面引入worker在src新建一个 worker.d.ts文件declaremodule'*.worker.ts'{classWebpackWorkerextendsWorker{constructor();}exportdefaultWebpackWorker;}在 tsconfig.json页面引入"lib":["esnext",......
  • REXROTH力士乐R900608575 VT-DFP-B-2X/G24K0/0/V
    REXROTH力士乐R900608575VT-DFP-B-2X/G24K0/0/V力士乐(Rexroth)VT11166-1X是一个电子放大器,用于液压控制系统中。这个型号属于力士乐的VT系列放大器。同系列的型号可能会有类似的功能和安装尺寸,但可能在电气参数、控制信号、输出电流或功率等级等方面有所不同。为了找到VT11......
  • Wpf BackgroundWorker WorkerSupportsCancellation CancellationPending
    //xaml<Windowx:Class="WpfApp37.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • GIS入门,EPSG:3857介绍,纯JS如何实现简化得Web墨卡托投影的逆变换和高精度Web墨卡托投影
    EPSG:3857坐标系介绍EPSG:3857坐标系,也称为Web墨卡托投影(WebMercatorprojection),是一种用于Web地图的常见投影系统。它是由谷歌地图在2005年引入并广泛采用的。这个投影系统将地球表面的经纬度坐标转换为平面坐标,使得地图在Web上的显示更加方便和流畅。EPSG:3857坐标系使......
  • Web墨卡托投影介绍,Web墨卡托投影和普通墨卡托投影有什么区别?EPSG:3857坐标系和EPSG:43
    Web墨卡托投影和普通墨卡托投影在本质上是相同的,但它们在坐标范围使用单位和应用领域上存在一些区别:坐标范围:普通墨卡托投影的坐标范围通常在整个地球表面上,由于使用浮点数表示,所以不限制其范围。Web墨卡托投影的坐标范围通常被限制在一个固定的范围内,以适应Web地图的显......