Skip to content

fix: update solution to lc problem: No.0857 #4332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,25 @@ class Solution:
class Solution {
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
int n = quality.length;
Pair[] t = new Pair[n];
Pair<Double, Integer>[] t = new Pair[n];
for (int i = 0; i < n; ++i) {
t[i] = new Pair(quality[i], wage[i]);
t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);
}
Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x));
Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey()));
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
double ans = 1e9;
double ans = 1e18;
int tot = 0;
for (var e : t) {
tot += e.q;
pq.offer(e.q);
tot += e.getValue();
pq.offer(e.getValue());
if (pq.size() == k) {
ans = Math.min(ans, tot * e.x);
ans = Math.min(ans, tot * e.getKey());
tot -= pq.poll();
}
}
return ans;
}
}

class Pair {
double x;
int q;

Pair(int q, int w) {
this.q = q;
this.x = (double) w / q;
}
}
```

#### C++
Expand All @@ -150,7 +140,7 @@ public:
}
sort(t.begin(), t.end());
priority_queue<int> pq;
double ans = 1e9;
double ans = 1e18;
int tot = 0;
for (auto& [x, q] : t) {
tot += q;
Expand All @@ -176,7 +166,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
}
sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
tot := 0
var ans float64 = 1e9
var ans float64 = 1e18
pq := hp{}
for _, e := range t {
tot += e.q
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,25 @@ class Solution:
class Solution {
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
int n = quality.length;
Pair[] t = new Pair[n];
Pair<Double, Integer>[] t = new Pair[n];
for (int i = 0; i < n; ++i) {
t[i] = new Pair(quality[i], wage[i]);
t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);
}
Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x));
Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey()));
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
double ans = 1e9;
double ans = 1e18;
int tot = 0;
for (var e : t) {
tot += e.q;
pq.offer(e.q);
tot += e.getValue();
pq.offer(e.getValue());
if (pq.size() == k) {
ans = Math.min(ans, tot * e.x);
ans = Math.min(ans, tot * e.getKey());
tot -= pq.poll();
}
}
return ans;
}
}

class Pair {
double x;
int q;

Pair(int q, int w) {
this.q = q;
this.x = (double) w / q;
}
}
```

#### C++
Expand All @@ -135,7 +125,7 @@ public:
}
sort(t.begin(), t.end());
priority_queue<int> pq;
double ans = 1e9;
double ans = 1e18;
int tot = 0;
for (auto& [x, q] : t) {
tot += q;
Expand All @@ -161,7 +151,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
}
sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
tot := 0
var ans float64 = 1e9
var ans float64 = 1e18
pq := hp{}
for _, e := range t {
tot += e.q
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Solution {
}
sort(t.begin(), t.end());
priority_queue<int> pq;
double ans = 1e9;
double ans = 1e18;
int tot = 0;
for (auto& [x, q] : t) {
tot += q;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
}
sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
tot := 0
var ans float64 = 1e9
var ans float64 = 1e18
pq := hp{}
for _, e := range t {
tot += e.q
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
class Solution {
public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
int n = quality.length;
Pair[] t = new Pair[n];
Pair<Double, Integer>[] t = new Pair[n];
for (int i = 0; i < n; ++i) {
t[i] = new Pair(quality[i], wage[i]);
t[i] = new Pair<>((double) wage[i] / quality[i], quality[i]);
}
Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x));
Arrays.sort(t, (a, b) -> Double.compare(a.getKey(), b.getKey()));
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
double ans = 1e9;
double ans = 1e18;
int tot = 0;
for (var e : t) {
tot += e.q;
pq.offer(e.q);
tot += e.getValue();
pq.offer(e.getValue());
if (pq.size() == k) {
ans = Math.min(ans, tot * e.x);
ans = Math.min(ans, tot * e.getKey());
tot -= pq.poll();
}
}
return ans;
}
}

class Pair {
double x;
int q;

Pair(int q, int w) {
this.q = q;
this.x = (double) w / q;
}
}
Loading