-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiringWorkers.java
More file actions
51 lines (38 loc) · 1.34 KB
/
Copy pathHiringWorkers.java
File metadata and controls
51 lines (38 loc) · 1.34 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
package LeetcodePractice;
import java.util.*;
public class HiringWorkers {
public long totalCost(int[] costs, int k, int candidates) {
int n = costs.length;
long total = 0;
int i = 0, j = n - 1;
PriorityQueue<Integer> left = new PriorityQueue<>();
PriorityQueue<Integer> right = new PriorityQueue<>();
for (int l = 0; l < candidates && i <= j; l++) {
left.offer(costs[i++]);
}
for (int r = 0; r < candidates && i <= j; r++) {
right.offer(costs[j--]);
}
for (int hireCount = 0; hireCount < k; hireCount++) {
if (right.isEmpty() || (!left.isEmpty() && left.peek() <= right.peek())) {
total += left.poll();
if (i <= j) {
left.offer(costs[i++]);
}
} else {
total += right.poll();
if (i <= j) {
right.offer(costs[j--]);
}
}
}
return total;
}
public static void main(String[] args) {
HiringWorkers hw = new HiringWorkers();
int[] costs1 = {17, 12, 10, 2, 7, 2, 11, 20, 8};
System.out.println(hw.totalCost(costs1, 3, 4));
int[] costs2 = {1, 2, 4, 1};
System.out.println(hw.totalCost(costs2, 3, 3));
}
}