Skip to content

Commit c5bfe3f

Browse files
authored
fix: update solution to lc problem: No.0857 (#4332)
1 parent 638d7eb commit c5bfe3f

File tree

5 files changed

+27
-57
lines changed

5 files changed

+27
-57
lines changed

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

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

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;

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,32 +1,22 @@
11
class Solution {
22
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
33
int n = quality.length;
4-
Pair[] t = new Pair[n];
4+
Pair<Double, Integer>[] t = new Pair[n];
55
for (int i = 0; i < n; ++i) {
6-
t[i] = new Pair(quality[i], wage[i]);
6+
t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);
77
}
8-
Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x));
8+
Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey()));
99
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
10-
double ans = 1e9;
10+
double ans = 1e18;
1111
int tot = 0;
1212
for (var e : t) {
13-
tot += e.q;
14-
pq.offer(e.q);
13+
tot += e.getValue();
14+
pq.offer(e.getValue());
1515
if (pq.size() == k) {
16-
ans = Math.min(ans, tot * e.x);
16+
ans = Math.min(ans, tot * e.getKey());
1717
tot -= pq.poll();
1818
}
1919
}
2020
return ans;
2121
}
2222
}
23-
24-
class Pair {
25-
double x;
26-
int q;
27-
28-
Pair(int q, int w) {
29-
this.q = q;
30-
this.x = (double) w / q;
31-
}
32-
}

0 commit comments

Comments
 (0)