Skip to content

Commit 8e1e9e1

Browse files
committed
Refactor mincostToHireWorkers method and update constants
1 parent c69127d commit 8e1e9e1

File tree

5 files changed

+39
-64
lines changed

5 files changed

+39
-64
lines changed

Diff for: solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md

+9-19
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,25 @@ class Solution:
106106
class Solution {
107107
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
108108
int n = quality.length;
109-
Pair[] t = new Pair[n];
109+
Pair<Double, Integer>[] t = new Pair[n];
110110
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]);
112112
}
113-
Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x));
113+
Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey()));
114114
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
115-
double ans = 1e9;
115+
double ans = 1e18;
116116
int tot = 0;
117117
for (var e : t) {
118-
tot += e.q;
119-
pq.offer(e.q);
118+
tot += e.getValue();
119+
pq.offer(e.getValue());
120120
if (pq.size() == k) {
121-
ans = Math.min(ans, tot * e.x);
121+
ans = Math.min(ans, tot * e.getKey());
122122
tot -= pq.poll();
123123
}
124124
}
125125
return ans;
126126
}
127127
}
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-
}
138128
```
139129

140130
#### C++
@@ -150,7 +140,7 @@ public:
150140
}
151141
sort(t.begin(), t.end());
152142
priority_queue<int> pq;
153-
double ans = 1e9;
143+
double ans = 1e18;
154144
int tot = 0;
155145
for (auto& [x, q] : t) {
156146
tot += q;
@@ -176,7 +166,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
176166
}
177167
sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
178168
tot := 0
179-
var ans float64 = 1e9
169+
var ans float64 = 1e18
180170
pq := hp{}
181171
for _, e := range t {
182172
tot += e.q

Diff for: solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md

+9-19
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,25 @@ class Solution:
9191
class Solution {
9292
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
9393
int n = quality.length;
94-
Pair[] t = new Pair[n];
94+
Pair<Double, Integer>[] t = new Pair[n];
9595
for (int i = 0; i < n; ++i) {
96-
t[i] = new Pair(quality[i], wage[i]);
96+
t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);
9797
}
98-
Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x));
98+
Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey()));
9999
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
100-
double ans = 1e9;
100+
double ans = 1e18;
101101
int tot = 0;
102102
for (var e : t) {
103-
tot += e.q;
104-
pq.offer(e.q);
103+
tot += e.getValue();
104+
pq.offer(e.getValue());
105105
if (pq.size() == k) {
106-
ans = Math.min(ans, tot * e.x);
106+
ans = Math.min(ans, tot * e.getKey());
107107
tot -= pq.poll();
108108
}
109109
}
110110
return ans;
111111
}
112112
}
113-
114-
class Pair {
115-
double x;
116-
int q;
117-
118-
Pair(int q, int w) {
119-
this.q = q;
120-
this.x = (double) w / q;
121-
}
122-
}
123113
```
124114

125115
#### C++
@@ -135,7 +125,7 @@ public:
135125
}
136126
sort(t.begin(), t.end());
137127
priority_queue<int> pq;
138-
double ans = 1e9;
128+
double ans = 1e18;
139129
int tot = 0;
140130
for (auto& [x, q] : t) {
141131
tot += q;
@@ -161,7 +151,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
161151
}
162152
sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
163153
tot := 0
164-
var ans float64 = 1e9
154+
var ans float64 = 1e18
165155
pq := hp{}
166156
for _, e := range t {
167157
tot += e.q

Diff for: solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Solution {
88
}
99
sort(t.begin(), t.end());
1010
priority_queue<int> pq;
11-
double ans = 1e9;
11+
double ans = 1e18;
1212
int tot = 0;
1313
for (auto& [x, q] : t) {
1414
tot += q;

Diff for: solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
55
}
66
sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
77
tot := 0
8-
var ans float64 = 1e9
8+
var ans float64 = 1e18
99
pq := hp{}
1010
for _, e := range t {
1111
tot += e.q
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
class Solution {
2-
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
3-
double ans = Double.MAX_VALUE;
4-
int qualitySum = 0;
5-
// (wagePerQuality, quality) sorted by wagePerQuality
6-
Pair<Double, Integer>[] workers = new Pair[quality.length];
7-
Queue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
8-
9-
for (int i = 0; i < quality.length; ++i)
10-
workers[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);
11-
12-
Arrays.sort(workers, (a, b) -> Double.compare(a.getKey(), b.getKey()));
13-
14-
for (Pair<Double, Integer> worker : workers) {
15-
final double wagePerQuality = worker.getKey();
16-
final int q = worker.getValue();
17-
maxHeap.offer(q);
18-
qualitySum += q;
19-
if (maxHeap.size() > k)
20-
qualitySum -= maxHeap.poll();
21-
if (maxHeap.size() == k)
22-
ans = Math.min(ans, qualitySum * wagePerQuality);
2+
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
3+
int n = quality.length;
4+
Pair<Double, Integer>[] t = new Pair[n];
5+
for (int i = 0; i < n; ++i) {
6+
t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);
7+
}
8+
Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey()));
9+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
10+
double ans = 1e18;
11+
int tot = 0;
12+
for (var e : t) {
13+
tot += e.getValue();
14+
pq.offer(e.getValue());
15+
if (pq.size() == k) {
16+
ans = Math.min(ans, tot * e.getKey());
17+
tot -= pq.poll();
18+
}
19+
}
20+
return ans;
2321
}
24-
25-
return ans;
26-
}
2722
}

0 commit comments

Comments
 (0)