Skip to content

Commit 36a8f75

Browse files
committed
https://leetcode.cn/problems/minimum-cost-to-hire-k-workers/
1 parent 1393236 commit 36a8f75

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/minimum-cost-to-hire-k-workers/
14+
1315
https://leetcode.cn/problems/number-of-closed-islands/
1416

1517
https://leetcode.cn/problems/find-servers-that-handled-most-number-of-requests/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default mincostToHireWorkers;
2+
function mincostToHireWorkers(
3+
quality: number[],
4+
wage: number[],
5+
k: number,
6+
): number {
7+
const pairs = quality
8+
.map((q, i) => [q, wage[i]])
9+
.sort((a, b) => a[1] / a[0] - b[1] / b[0]);
10+
let ans = Infinity;
11+
let totalq = 0;
12+
const h = new PriorityQueue<number>((a, b) => a - b);
13+
14+
for (const [i, [q, w]] of pairs.entries()) {
15+
totalq += q;
16+
h.enqueue(-q);
17+
if (i >= k - 1) {
18+
ans = Math.min(ans, (w / q) * totalq);
19+
totalq += h.dequeue();
20+
}
21+
}
22+
return ans;
23+
}
24+
25+
import { PriorityQueue } from "https://esm.sh/@datastructures-js/[email protected]";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { assertEquals } from "asserts";
2+
import mincostToHireWorkers from "./index.ts";
3+
Deno.test("minimum-cost-to-hire-k-workers", () => {
4+
const cases: [quality: number[], wage: number[], k: number, k: number][] = [
5+
[[10, 20, 5], [70, 50, 30], 2, 105.0],
6+
[[3, 1, 10, 10, 1], [4, 8, 2, 2, 7], 3, 30.66667],
7+
];
8+
cases.forEach(([a, b, c, d]) => {
9+
assertEquals(mincostToHireWorkers(a, b, c).toFixed(5), d.toFixed(5));
10+
});
11+
});

0 commit comments

Comments
 (0)