Skip to content

Commit 3e7359c

Browse files
committed
https://leetcode.cn/problems/possible-bipartition/
1 parent 8a9ac98 commit 3e7359c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-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/possible-bipartition/
14+
1315
https://leetcode.cn/problems/fruit-into-baskets/
1416

1517
https://leetcode.cn/problems/min-cost-to-connect-all-points/

possible-bipartition/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { UnionFind } from "../largest-component-size-by-common-factor/UnionFind.ts";
2+
export default function possibleBipartition(
3+
n: number,
4+
dislikes: number[][]
5+
): boolean {
6+
const uf = new UnionFind();
7+
8+
const e = Array<Array<number>>(n + 1);
9+
10+
for (const [a, b] of dislikes) {
11+
e[a] ??= [];
12+
e[b] ??= [];
13+
e[a].push(b);
14+
e[b].push(a);
15+
}
16+
17+
for (const i of Object.keys(e)) {
18+
for (const d of e[Number(i)]) {
19+
uf.union(d, e[Number(i)][0]);
20+
if (uf.connected(d, Number(i))) return false;
21+
}
22+
}
23+
24+
return true;
25+
}

0 commit comments

Comments
 (0)