-
Notifications
You must be signed in to change notification settings - Fork 5
/
MinimumCostToHireKWorkers.java
46 lines (42 loc) · 1.22 KB
/
MinimumCostToHireKWorkers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*https://leetcode.com/problems/minimum-cost-to-hire-k-workers/*/
class Worker implements Comparable<Worker>
{
public int quality, wage;
public double ratio;
public Worker(int q, int w)
{
quality = q;
wage = w;
ratio = (double)wage/(double)quality;
}
@Override
public int compareTo(Worker other)
{
double result = this.ratio-other.ratio;
if (result < 0) return -1;
if (result > 0) return 1;
return 0;
}
}
class Solution {
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
int n = quality.length, i;
Worker[] workers = new Worker[n];
for (i = 0; i < n; ++i)
workers[i] = new Worker(quality[i],wage[i]);
Arrays.sort(workers);
double result = 1e9;
int sum = 0;
PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
for (Worker worker : workers)
{
heap.add(-worker.quality);
sum += worker.quality;
if (heap.size() > k)
sum += heap.poll();
if (heap.size() == k)
result = Math.min(result,sum*worker.ratio);
}
return result;
}
}