Skip to content

Commit 101b3cb

Browse files
authored
Create Solution.java
1 parent 38a298a commit 101b3cb

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public double mincostToHireWorkers(int[] quality, int[] wage, int K) {
3+
Worker[] workers = new Worker[quality.length];
4+
for (int i = 0; i < quality.length; ++i) {
5+
workers[i] = new Worker((double) wage[i] / quality[i], quality[i]);
6+
}
7+
Arrays.sort(workers);
8+
double res = 1e9;
9+
Queue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
10+
int s = 0;
11+
for (Worker worker : workers) {
12+
s += worker.quality;
13+
queue.offer(worker.quality);
14+
if (queue.size() > K) {
15+
s -= queue.poll();
16+
}
17+
if (queue.size() == K) {
18+
res = Math.min(res, s * worker.x);
19+
}
20+
}
21+
return res;
22+
}
23+
24+
class Worker implements Comparable<Worker> {
25+
double x;
26+
int quality;
27+
28+
public Worker(double x, int quality) {
29+
this.x = x;
30+
this.quality = quality;
31+
}
32+
33+
@Override
34+
public int compareTo(Worker o) {
35+
return Double.compare(x, o.x);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)