-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumCostToHireKWorkers.java
More file actions
54 lines (42 loc) · 1.47 KB
/
MinimumCostToHireKWorkers.java
File metadata and controls
54 lines (42 loc) · 1.47 KB
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
47
48
49
50
51
52
53
54
package priorityQueue;
import java.util.*;
public class MinimumCostToHireKWorkers {
public static double minCostToHireWorkers(int[] quality, int[] wage, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
List<Pair> ratio = new ArrayList<>();
int n = quality.length, qualitySum = 0;
double res, maxRate = 0.0;
for (int i = 0; i < n; ++i) {
ratio.add(new Pair((double) wage[i] / quality[i], quality[i]));
}
ratio.sort(Comparator.comparingDouble(Pair::getKey));
for (int i = 0; i < k; ++i) {
qualitySum += ratio.get(i).getValue();
maxRate = Math.max(maxRate, ratio.get(i).getKey());
maxHeap.offer(ratio.get(i).getValue());
}
res = maxRate * qualitySum;
for (int i = k; i < n; ++i) {
maxRate = Math.max(maxRate, ratio.get(i).getKey());
qualitySum -= maxHeap.poll();
qualitySum += ratio.get(i).getValue();
maxHeap.offer(ratio.get(i).getValue());
res = Math.min(res, maxRate * qualitySum);
}
return res;
}
static class Pair {
double key;
Integer value;
public Pair(double key, Integer value) {
this.key = key;
this.value = value;
}
public double getKey() {
return key;
}
public Integer getValue() {
return value;
}
}
}