Problem
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, where N = quality.length = wage.length
1 <= quality[i] <= 10000
1 <= wage[i] <= 10000
Answers within 10^-5 of the correct answer will be considered correct.
class Solution { public double mincostToHireWorkers(int[] quality, int[] wage, int K) { int n = quality.length; double[][] workers = new double[n][2]; for (int i = 0; i < n; i++) { double ratio = (double) wage[i] / quality[i]; workers[i] = new double[]{ratio, (double) quality[i]}; } Arrays.sort(workers, (a, b)->Double.compare(a[0], b[0])); PriorityQueuepq = new PriorityQueue<>((a, b)->Double.compare(b, a)); double total = Double.MAX_VALUE, temp = 0; for (double[] worker: workers) { temp += worker[1]; pq.offer(worker[1]); if (pq.size() > K) { double maxQuality = pq.poll(); temp -= maxQuality; } if (pq.size() == K) { double curTotal = temp*worker[0]; total = Math.min(total, curTotal); } } return total; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/72540.html
摘要:動態規劃復雜度時間空間思路直到房子,其最小的涂色開銷是直到房子的最小涂色開銷,加上房子本身的涂色開銷。我們在原數組上修改,可以做到不用空間。代碼找出最小和次小的,最小的要記錄下標,方便下一輪判斷 Paint House There are a row of n houses, each house can be painted with one of the three colors...
摘要:題目解答利用的思路,只不過把三種顏色換成了種顏色,所以是如何把它的復雜度降到那么就是如果將顏色的部分只掃一遍。參考的里只需要記錄下每個的最小的兩個顏色。 題目:There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house w...
摘要:同時你可以選擇從第階開始或者第一階開始。我們的目標是找出你爬到最頂層所付出的最小的開銷。最低開銷是,從第階開始,只花費就可以到頂層。想法動態規劃問題。的長度應該為數組長度加一,其中數組的最后一個元素的值就是本題的答案,最低開銷。 題目詳情 On a staircase, the i-th step has some non-negative cost cost[i] assigned ...
摘要:在原數組上動規,每一行對應一個房子,每一個元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個元素該元素的值上一行兩個與該元素不同列元素的值的較小者。不過這次要記錄三個變量本行最小值,本行第二小值,本行最小值下標。 Paint House Problem There are a row of n houses, each house can be painted ...
閱讀 2485·2021-11-24 09:39
閱讀 3528·2019-08-30 15:53
閱讀 602·2019-08-29 15:15
閱讀 2909·2019-08-26 13:23
閱讀 3224·2019-08-26 10:48
閱讀 650·2019-08-26 10:31
閱讀 776·2019-08-26 10:30
閱讀 2370·2019-08-23 18:32