Skip to content

Commit 2ab982d

Browse files
committed
Solution to today's problem
1 parent ce2934f commit 2ab982d

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/main/java/io/github/spannm/leetcode/lc0/lc0800/Problem0857.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,41 @@
22

33
import io.github.spannm.leetcode.LeetcodeProblem;
44

5-
import java.util.Arrays;
6-
import java.util.Comparator;
75
import java.util.PriorityQueue;
6+
import java.util.Queue;
7+
import java.util.stream.IntStream;
88

99
/**
1010
* 857. Minimum Cost to Hire K Workers.
1111
*/
1212
class Problem0857 extends LeetcodeProblem {
1313

1414
double mincostToHireWorkers(int[] _quality, int[] _wage, int _k) {
15-
int n = _quality.length;
16-
Pair[] t = new Pair[n];
17-
for (int i = 0; i < n; i++) {
18-
t[i] = new Pair(_quality[i], _wage[i]);
19-
}
20-
Arrays.sort(t, Comparator.comparingDouble(a -> a.x));
21-
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
22-
double ans = 1e9;
15+
Worker[] workers = IntStream.range(0, _quality.length)
16+
.mapToObj(i -> new Worker(_quality[i], _wage[i]))
17+
.sorted((_w1, _w2) -> Double.compare(_w1.wagePerQ, _w2.wagePerQ))
18+
.toArray(Worker[]::new);
19+
Queue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
20+
double amt = 1e9;
2321
int tot = 0;
24-
for (var e : t) {
25-
tot += e.q;
26-
pq.offer(e.q);
22+
for (var w : workers) {
23+
tot += w.quality;
24+
pq.offer(w.quality);
2725
if (pq.size() == _k) {
28-
ans = Math.min(ans, tot * e.x);
26+
amt = Math.min(amt, tot * w.wagePerQ);
2927
tot -= pq.poll();
3028
}
3129
}
32-
return ans;
30+
return amt;
3331
}
3432

35-
static class Pair {
36-
private final double x;
37-
private final int q;
33+
static class Worker {
34+
private final int quality; // quality
35+
private final double wagePerQ; // wage per quality
3836

39-
Pair(int _q, int _w) {
40-
q = _q;
41-
x = (double) _w / _q;
37+
Worker(int _q, int _w) {
38+
quality = _q;
39+
wagePerQ = (double) _w / _q;
4240
}
4341

4442
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.spannm.leetcode.lc0.lc0800;
2+
3+
import io.github.spannm.leetcode.LeetcodeBaseTest;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.converter.ConvertWith;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
class Problem0857Test extends LeetcodeBaseTest {
9+
10+
@ParameterizedTest(name = "[{index}] {0}; {1}; {2} --> {3}")
11+
@CsvSource(delimiter = ';', value = {
12+
"10,20,5; 70,50,30,; 2; 105.00",
13+
"3,1,10,10,1; 4,8,2,2,7; 3; 30.66667"
14+
})
15+
void test(
16+
@ConvertWith(CsvToIntArray.class) int[] _quality,
17+
@ConvertWith(CsvToIntArray.class) int[] _wage,
18+
int _k,
19+
double _expectedResult) {
20+
assertEquals(_expectedResult, new Problem0857().mincostToHireWorkers(_quality, _wage, _k), 0.00001);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)