File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/possible-bipartition/
14
+
13
15
https://leetcode.cn/problems/fruit-into-baskets/
14
16
15
17
https://leetcode.cn/problems/min-cost-to-connect-all-points/
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments