@@ -106,35 +106,25 @@ class Solution:
106
106
class Solution {
107
107
public double mincostToHireWorkers (int [] quality , int [] wage , int k ) {
108
108
int n = quality. length;
109
- Pair [] t = new Pair [n];
109
+ Pair< Double , Integer > [] t = new Pair [n];
110
110
for (int i = 0 ; i < n; ++ i) {
111
- t[i] = new Pair ( quality[i], wage [i]);
111
+ t[i] = new Pair<> (( double ) wage[i] / quality[i], quality [i]);
112
112
}
113
- Arrays . sort(t, (a, b) - > Double . compare(a. x , b. x ));
113
+ Arrays . sort(t, (a, b) - > Double . compare(a. getKey() , b. getKey() ));
114
114
PriorityQueue<Integer > pq = new PriorityQueue<> ((a, b) - > b - a);
115
- double ans = 1e9 ;
115
+ double ans = 1e18 ;
116
116
int tot = 0 ;
117
117
for (var e : t) {
118
- tot += e. q ;
119
- pq. offer(e. q );
118
+ tot += e. getValue() ;
119
+ pq. offer(e. getValue() );
120
120
if (pq. size() == k) {
121
- ans = Math . min(ans, tot * e. x );
121
+ ans = Math . min(ans, tot * e. getKey() );
122
122
tot -= pq. poll();
123
123
}
124
124
}
125
125
return ans;
126
126
}
127
127
}
128
-
129
- class Pair {
130
- double x;
131
- int q;
132
-
133
- Pair (int q , int w ) {
134
- this . q = q;
135
- this . x = (double ) w / q;
136
- }
137
- }
138
128
```
139
129
140
130
#### C++
@@ -150,7 +140,7 @@ public:
150
140
}
151
141
sort(t.begin(), t.end());
152
142
priority_queue<int > pq;
153
- double ans = 1e9 ;
143
+ double ans = 1e18 ;
154
144
int tot = 0;
155
145
for (auto& [ x, q] : t) {
156
146
tot += q;
@@ -176,7 +166,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
176
166
}
177
167
sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
178
168
tot := 0
179
- var ans float64 = 1e9
169
+ var ans float64 = 1e18
180
170
pq := hp{}
181
171
for _, e := range t {
182
172
tot += e.q
0 commit comments