|
4 | 4 |
|
5 | 5 | public class Main {
|
6 | 6 |
|
7 |
| - public static class Solution { |
| 7 | + static class Man { |
| 8 | + int quality; |
| 9 | + int wage; |
8 | 10 |
|
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 | + } |
17 | 15 |
|
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; |
21 | 27 | }
|
| 28 | + }); |
| 29 | + for (int i = 0; i < quality.length; i++) { |
| 30 | + queue.offer(new Man(quality[i], wage[i])); |
22 | 31 | }
|
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; |
24 | 46 | }
|
25 |
| - |
26 | 47 | }
|
27 | 48 |
|
28 | 49 | public static void main(String[] args) {
|
29 | 50 | Solution solution = new Solution();
|
30 | 51 |
|
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); |
44 | 57 |
|
| 58 | + System.out.println(s); |
45 | 59 | }
|
46 | 60 | }
|
0 commit comments