Skip to content

Commit 05cd469

Browse files
committed
good problem
1 parent 0530dc0 commit 05cd469

File tree

3 files changed

+97
-27
lines changed

3 files changed

+97
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,5 +393,6 @@
393393
|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Java](leetcode/solution/src/KthLargestElementInAStream.java)|100||
394394
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
395395
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Java](leetcode/solution/src/MostCommonWord.java)|85||
396+
|857|[Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Java](leetcode/solution/src/MinimumCostToHireKWorkers.java|70||
396397
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
397398
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Java](leetcode/solution/src/UniqueEmailAddresses.java)|90||
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.Comparator;
2+
import java.util.PriorityQueue;
3+
import java.util.Queue;
4+
5+
public class MinimumCostToHireKWorkers {
6+
7+
class Man {
8+
int quality;
9+
int wage;
10+
11+
Man(int a, int b) {
12+
quality = a;
13+
wage = b;
14+
}
15+
16+
double ratio() {
17+
return (double) wage / quality;
18+
}
19+
}
20+
21+
/**
22+
* 这题意思是选取K个工人,保证付给他们的工资和quality的比例都是一样的,且不低于他们的期望工资,问最少付的工资是多少
23+
* 定义性价比为wage/quality,这个值越小表示性价比越高
24+
* 将所有工人按性价比排序,显然要少付工资,当然尽可能选取更多的高性价比工人
25+
* 为了满足所有人工资和quality比例都一样,这个比例显然是所选的K个工人中性价比最低的那个
26+
* 不能完全按性价比来,因为可能存在的情况是高性价比quality也高,在ratio一定的情况下,要付出的总工资也多
27+
* 所以要对性价比从高到低地遍历工人,边遍历边当满足K个人数的时候计算要付出的工资,人数超的时候踢掉quality最高的那个人,因为越往后遍历ratio越大,那么总体的quality
28+
* 要尽可能小才行
29+
*/
30+
public double mincostToHireWorkers(int[] quality, int[] wage, int K) {
31+
Queue<Man> queue = new PriorityQueue<>(new Comparator<Man>() {
32+
@Override
33+
public int compare(Man o1, Man o2) {
34+
return o1.ratio() > o2.ratio() ? 1 : -1;
35+
}
36+
});
37+
for (int i = 0; i < quality.length; i++) {
38+
queue.offer(new Man(quality[i], wage[i]));
39+
}
40+
double money = Integer.MAX_VALUE, qsum = 0;
41+
Queue<Integer> queue2 = new PriorityQueue<>(Comparator.reverseOrder());
42+
while (!queue.isEmpty()) {
43+
Man man = queue.poll();
44+
qsum += man.quality;
45+
queue2.offer(man.quality);
46+
if (queue2.size() > K) {
47+
qsum -= queue2.poll();
48+
}
49+
if (queue2.size() == K) {
50+
money = Math.min(money, qsum * man.ratio());
51+
}
52+
}
53+
return money;
54+
}
55+
}

leetcode/src/Main.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,57 @@
44

55
public class Main {
66

7-
public static class Solution {
7+
static class Man {
8+
int quality;
9+
int wage;
810

9-
public int kEmptySlots(int[] flowers, int k) {
10-
int[] days = new int[flowers.length];
11-
for (int i = 0; i < flowers.length; i++) days[flowers[i] - 1] = i + 1;
12-
int left = 0, right = k + 1, res = Integer.MAX_VALUE;
13-
for (int i = left + 1; i <= right && right < days.length; i++) {
14-
if (i == right) {
15-
res = Math.min(res, Math.max(days[left], days[right]));
16-
}
11+
Man(int a, int b) {
12+
quality = a;
13+
wage = b;
14+
}
1715

18-
if (days[i] < days[left] || days[i] < days[right]) {
19-
left = i;
20-
right = k + 1 + i;
16+
double ratio() {
17+
return (double) wage / quality;
18+
}
19+
}
20+
21+
public static class Solution {
22+
public double mincostToHireWorkers(int[] quality, int[] wage, int K) {
23+
Queue<Man> queue = new PriorityQueue<>(new Comparator<Man>() {
24+
@Override
25+
public int compare(Man o1, Man o2) {
26+
return o1.ratio() > o2.ratio() ? 1 : -1;
2127
}
28+
});
29+
for (int i = 0; i < quality.length; i++) {
30+
queue.offer(new Man(quality[i], wage[i]));
2231
}
23-
return (res == Integer.MAX_VALUE) ? -1 : res;
32+
double money = Integer.MAX_VALUE, qsum = 0;
33+
Queue<Integer> queue2 = new PriorityQueue<>(Comparator.reverseOrder());
34+
while (!queue.isEmpty()) {
35+
Man man = queue.poll();
36+
qsum += man.quality;
37+
queue2.offer(man.quality);
38+
if (queue2.size() > K) {
39+
qsum -= queue2.poll();
40+
}
41+
if (queue2.size() == K) {
42+
money = Math.min(money, qsum * man.ratio());
43+
}
44+
}
45+
return money;
2446
}
25-
2647
}
2748

2849
public static void main(String[] args) {
2950
Solution solution = new Solution();
3051

31-
TreeMap<Integer, Boolean> map = new TreeMap<>();
32-
int[] nums = {4, 1, 7, 5, 2, 8, 10, 0};
33-
for (int n : nums) {
34-
map.put(n, true);
35-
}
36-
for (int k : map.keySet()) {
37-
System.out.print(k + " ");
38-
}
39-
40-
int n = solution.kEmptySlots(new int[]{
41-
1, 2, 3
42-
}, 1);
43-
System.out.println(n);
52+
double s = solution.mincostToHireWorkers(new int[] {
53+
3,1,10,10,1
54+
}, new int[] {
55+
4,8,2,2,7
56+
},3);
4457

58+
System.out.println(s);
4559
}
4660
}

0 commit comments

Comments
 (0)