1
+ // Runtime: 1259 ms (Top 62.50%) | Memory: 51.1 MB (Top 100.00%)
1
2
/*
2
3
DSU Class Template
3
4
*/
@@ -6,20 +7,20 @@ class DSU {
6
7
this . parents = new Map ( ) ;
7
8
this . rank = new Map ( ) ;
8
9
}
9
-
10
+
10
11
add ( x ) {
11
12
this . parents . set ( x , x ) ;
12
13
this . rank . set ( x , 0 ) ;
13
14
}
14
-
15
+
15
16
find ( x ) {
16
17
const parent = this . parents . get ( x ) ;
17
18
if ( parent === x ) return x ;
18
19
const setParent = this . find ( parent ) ;
19
20
this . parents . set ( x , setParent ) ;
20
21
return setParent ;
21
22
}
22
-
23
+
23
24
union ( x , y ) {
24
25
const xParent = this . find ( x ) , yParent = this . find ( y ) ;
25
26
const xRank = this . rank . get ( xParent ) , yRank = this . rank . get ( yParent ) ;
@@ -31,7 +32,7 @@ class DSU {
31
32
} else {
32
33
this . parents . set ( xParent , yParent ) ;
33
34
this . rank . set ( yParent , yRank + 1 ) ;
34
- }
35
+ }
35
36
}
36
37
}
37
38
@@ -41,7 +42,7 @@ Friend Requests
41
42
var friendRequests = function ( n , restrictions , requests ) {
42
43
const dsu = new DSU ( ) , result = [ ] ;
43
44
for ( let i = 0 ; i < n ; i ++ ) dsu . add ( i ) ;
44
-
45
+
45
46
for ( let [ friend1 , friend2 ] of requests ) {
46
47
const parent1 = dsu . find ( friend1 ) , parent2 = dsu . find ( friend2 ) ;
47
48
let friendshipPossible = true ;
@@ -58,4 +59,4 @@ var friendRequests = function(n, restrictions, requests) {
58
59
result . push ( friendshipPossible ) ;
59
60
}
60
61
return result ;
61
- } ;
62
+ } ;
0 commit comments