Skip to content

Commit 91fcdd6

Browse files
committed
https://leetcode.cn/problems/number-of-good-paths/
1 parent c68441b commit 91fcdd6

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-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/number-of-good-paths/
14+
1315
https://leetcode.cn/problems/count-all-possible-routes/
1416

1517
https://leetcode.cn/problems/sort-the-people/

largest-component-size-by-common-factor/UnionFind.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export class UnionFind<T = number> {
99
}
1010
return this.#parents.get(x) ?? x;
1111
}
12+
size(x: T): number {
13+
return (this.#sizes.get(this.find(x)) ?? 1);
14+
}
1215
connected(p: T, q: T): boolean {
1316
return this.find(p) == this.find(q);
1417
}

number-of-good-paths/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { UnionFind } from "../largest-component-size-by-common-factor/UnionFind.ts";
2+
function numberOfGoodPaths(vals: number[], edges: number[][]): number {
3+
const n = vals.length;
4+
const cnt = new Map<number, number[]>();
5+
6+
for (const [i, v] of vals.entries()) {
7+
const arr = cnt.get(v) ?? [];
8+
arr.push(i);
9+
cnt.set(v, arr);
10+
}
11+
edges.sort((a, b) =>
12+
Math.max(vals[a[0]], vals[a[1]]) - Math.max(vals[b[0]], vals[b[1]])
13+
);
14+
const uf = new UnionFind();
15+
let ans = n;
16+
let j = 0;
17+
18+
for (const [val, vec] of [...cnt.entries()].sort((a, b) => a[0] - b[0])) {
19+
while (
20+
j < edges.length && vals[edges[j][0]] <= val &&
21+
vals[edges[j][1]] <= val
22+
) {
23+
uf.union(edges[j][0], edges[j][1]);
24+
j++;
25+
}
26+
27+
const count = new Map<number, number>();
28+
29+
for (const v of vec) {
30+
const key = uf.find(v);
31+
count.set(key, (count.get(key) ?? 0) + 1);
32+
}
33+
34+
ans += [...count.values()].reduce(
35+
(p, c) => p + Math.floor(c * (c - 1) / 2),
36+
0,
37+
);
38+
}
39+
return ans;
40+
}
41+
export default numberOfGoodPaths;

number-of-good-paths/test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { assertEquals } from "asserts";
2+
import numberOfGoodPaths from "./index.ts";
3+
Deno.test("number-of-good-paths", () => {
4+
const inputs: [vals: number[], edges: number[][]][] = [
5+
[[1, 3, 2, 1, 3], [[0, 1], [0, 2], [2, 3], [2, 4]]],
6+
[[1, 1, 2, 2, 3], [[0, 1], [1, 2], [2, 3], [2, 4]]],
7+
[[1], []],
8+
[[2, 5, 5, 1, 5, 2, 3, 5, 1, 5], [
9+
[0, 1],
10+
[2, 1],
11+
[3, 2],
12+
[3, 4],
13+
[3, 5],
14+
[5, 6],
15+
[1, 7],
16+
[8, 4],
17+
[
18+
9,
19+
7,
20+
],
21+
]],
22+
];
23+
const outputs = [6, 7, 1, 20];
24+
assertEquals(inputs.map((a) => numberOfGoodPaths(a[0], a[1])), outputs);
25+
});

0 commit comments

Comments
 (0)