-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinimumCostToHireKWorkersProblem.java
62 lines (48 loc) · 1.61 KB
/
MinimumCostToHireKWorkersProblem.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main.com.sumit.coding.companies.google.arraysAndStrings;
import java.util.Arrays;
import java.util.PriorityQueue;
/*
* Problem URL : https://leetcode.com/problems/minimum-cost-to-hire-k-workers/
* */
public class MinimumCostToHireKWorkersProblem {
public static void main(String[] args) {
int[] quality = {10, 20, 5};
int[] wage = {70, 50, 30};
int k = 2;
System.out.println(new MinimumCostToHireKWorkersProblem().mincostToHireWorkers(quality, wage, k));
}
/*
* Time Complexity : O(N logN)
* Space Complexity : O(N)
* N is number of workers
* */
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
int len = quality.length;
Worker[] workers = new Worker[len];
for (int i = 0; i < len; i++) workers[i] = new Worker(quality[i], wage[i]);
Arrays.sort(workers);
double ans = Integer.MAX_VALUE;
int sumq = 0;
PriorityQueue<Integer> pool = new PriorityQueue<>();
for (Worker worker : workers) {
pool.offer(-worker.quality);
sumq += worker.quality;
if (pool.size() > k) sumq += pool.poll();
if (pool.size() == k) ans = Math.min(ans, sumq * worker.ratio());
}
return ans;
}
}
class Worker implements Comparable<Worker> {
public int quality, wage;
public Worker(int q, int w) {
quality = q;
wage = w;
}
public double ratio() {
return (double) wage / quality;
}
public int compareTo(Worker worker) {
return Double.compare(ratio(), worker.ratio());
}
}