File tree 1 file changed +52
-0
lines changed
1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n
3
+ * @param {number[][] } meetings
4
+ * @param {number } firstPerson
5
+ * @return {number[] }
6
+ */
7
+ const findAllPeople = function ( n , meetings , firstPerson ) {
8
+ meetings . sort ( ( a , b ) => a [ 2 ] - b [ 2 ] )
9
+ const uf = new UnionFind ( n ) ;
10
+ uf . connect ( 0 , firstPerson ) ;
11
+ let ppl = [ ] ;
12
+ for ( let i = 0 , len = meetings . length ; i < len ; ) {
13
+ ppl = [ ] ;
14
+ let time = meetings [ i ] [ 2 ] ;
15
+ while ( i < len && meetings [ i ] [ 2 ] === time ) {
16
+ uf . connect ( meetings [ i ] [ 0 ] , meetings [ i ] [ 1 ] ) ;
17
+ ppl . push ( meetings [ i ] [ 0 ] ) ;
18
+ ppl . push ( meetings [ i ] [ 1 ] ) ;
19
+ i ++
20
+ }
21
+ for ( let n of ppl ) {
22
+ if ( ! uf . connected ( 0 , n ) ) uf . reset ( n ) ;
23
+ }
24
+ }
25
+ let ans = [ ] ;
26
+ for ( let i = 0 ; i < n ; ++ i ) {
27
+ if ( uf . connected ( 0 , i ) ) ans . push ( i ) ;
28
+ }
29
+ return ans ;
30
+ } ;
31
+
32
+ class UnionFind {
33
+ constructor ( n ) {
34
+ this . arr = Array ( n ) . fill ( null )
35
+ this . arr . forEach ( ( e , i , arr ) => arr [ i ] = i )
36
+ }
37
+ connect ( a , b ) {
38
+ this . arr [ this . find ( a ) ] = this . find ( this . arr [ b ] )
39
+ }
40
+ find ( a ) {
41
+ return this . arr [ a ] === a ? a : ( this . arr [ a ] = this . find ( this . arr [ a ] ) )
42
+ }
43
+ connected ( a , b ) {
44
+ return this . find ( a ) === this . find ( b )
45
+ }
46
+ reset ( a ) {
47
+ this . arr [ a ] = a
48
+ }
49
+ }
50
+
51
+ // another
52
+
1
53
/**
2
54
* @param {number } n
3
55
* @param {number[][] } meetings
You can’t perform that action at this time.
0 commit comments