File tree 15 files changed +1043
-136
lines changed
0600-0699/0684.Redundant Connection
1200-1299/1258.Synonymous Sentences
15 files changed +1043
-136
lines changed Load Diff Large diffs are not rendered by default.
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
- vector<int > p;
4
-
5
3
vector<int > findRedundantConnection (vector<vector<int >>& edges) {
6
- p.resize (1010 );
7
- for (int i = 0 ; i < p.size (); ++i) p[i] = i;
8
- for (auto & e : edges) {
9
- int a = e[0 ], b = e[1 ];
10
- if (find (a) == find (b)) return e;
11
- p[find (a)] = find (b);
4
+ int n = edges.size ();
5
+ vector<int > p (n);
6
+ iota (p.begin (), p.end (), 0 );
7
+ function<int (int )> find = [&](int x) {
8
+ return x == p[x] ? x : p[x] = find (p[x]);
9
+ };
10
+ for (int i = 0 ;; ++i) {
11
+ int pa = find (edges[i][0 ] - 1 );
12
+ int pb = find (edges[i][1 ] - 1 );
13
+ if (pa == pb) {
14
+ return edges[i];
15
+ }
16
+ p[pa] = pb;
12
17
}
13
- return {};
14
18
}
15
-
16
- int find (int x) {
17
- if (p[x] != x) p[x] = find (p[x]);
18
- return p[x];
19
- }
20
- };
19
+ };
Original file line number Diff line number Diff line change 1
1
func findRedundantConnection (edges [][]int ) []int {
2
- p := make ([]int , 1010 )
2
+ n := len (edges )
3
+ p := make ([]int , n )
3
4
for i := range p {
4
5
p [i ] = i
5
6
}
6
- var find func (x int ) int
7
+ var find func (int ) int
7
8
find = func (x int ) int {
8
9
if p [x ] != x {
9
10
p [x ] = find (p [x ])
10
11
}
11
12
return p [x ]
12
13
}
13
- for _ , e := range edges {
14
- a , b := e [ 0 ], e [ 1 ]
15
- if find ( a ) == find ( b ) {
16
- return e
14
+ for i := 0 ; ; i ++ {
15
+ pa , pb := find ( edges [ i ][ 0 ] - 1 ), find ( edges [ i ][ 1 ] - 1 )
16
+ if pa == pb {
17
+ return edges [ i ]
17
18
}
18
- p [find ( a ) ] = find ( b )
19
+ p [pa ] = pb
19
20
}
20
- return []int {}
21
- }
21
+ }
Original file line number Diff line number Diff line change @@ -2,18 +2,19 @@ class Solution {
2
2
private int [] p ;
3
3
4
4
public int [] findRedundantConnection (int [][] edges ) {
5
- p = new int [1010 ];
6
- for (int i = 0 ; i < p .length ; ++i ) {
5
+ int n = edges .length ;
6
+ p = new int [n ];
7
+ for (int i = 0 ; i < n ; ++i ) {
7
8
p [i ] = i ;
8
9
}
9
- for (int [] e : edges ) {
10
- int a = e [0 ], b = e [1 ];
11
- if (find (a ) == find (b )) {
12
- return e ;
10
+ for (int i = 0 ;; ++i ) {
11
+ int pa = find (edges [i ][0 ] - 1 );
12
+ int pb = find (edges [i ][1 ] - 1 );
13
+ if (pa == pb ) {
14
+ return edges [i ];
13
15
}
14
- p [find ( a ) ] = find ( b ) ;
16
+ p [pa ] = pb ;
15
17
}
16
- return null ;
17
18
}
18
19
19
20
private int find (int x ) {
@@ -22,4 +23,4 @@ private int find(int x) {
22
23
}
23
24
return p [x ];
24
25
}
25
- }
26
+ }
Original file line number Diff line number Diff line change 3
3
* @return {number[] }
4
4
*/
5
5
var findRedundantConnection = function ( edges ) {
6
- let p = Array . from ( { length : 1010 } , ( _ , i ) => i ) ;
7
- function find ( x ) {
8
- if ( p [ x ] != x ) {
6
+ const n = edges . length ;
7
+ const p = Array . from ( { length : n } , ( _ , i ) => i ) ;
8
+ const find = x => {
9
+ if ( p [ x ] !== x ) {
9
10
p [ x ] = find ( p [ x ] ) ;
10
11
}
11
12
return p [ x ] ;
12
- }
13
- for ( let [ a , b ] of edges ) {
14
- if ( find ( a ) == find ( b ) ) {
15
- return [ a , b ] ;
13
+ } ;
14
+ for ( let i = 0 ; ; ++ i ) {
15
+ const pa = find ( edges [ i ] [ 0 ] - 1 ) ;
16
+ const pb = find ( edges [ i ] [ 1 ] - 1 ) ;
17
+ if ( pa === pb ) {
18
+ return edges [ i ] ;
16
19
}
17
- p [ find ( a ) ] = find ( b ) ;
20
+ p [ pa ] = pb ;
18
21
}
19
- return [ ] ;
20
22
} ;
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def findRedundantConnection (self , edges : List [List [int ]]) -> List [int ]:
3
- def find (x ) :
3
+ def find (x : int ) -> int :
4
4
if p [x ] != x :
5
5
p [x ] = find (p [x ])
6
6
return p [x ]
7
7
8
- p = list (range (1010 ))
8
+ p = list (range (len ( edges ) ))
9
9
for a , b in edges :
10
- if find (a ) == find (b ):
10
+ pa , pb = find (a - 1 ), find (b - 1 )
11
+ if pa == pb :
11
12
return [a , b ]
12
- p [find (a )] = find (b )
13
- return []
13
+ p [pa ] = pb
Original file line number Diff line number Diff line change
1
+ function findRedundantConnection ( edges : number [ ] [ ] ) : number [ ] {
2
+ const n = edges . length ;
3
+ const p : number [ ] = Array . from ( { length : n } , ( _ , i ) => i ) ;
4
+ const find = ( x : number ) : number => {
5
+ if ( p [ x ] !== x ) {
6
+ p [ x ] = find ( p [ x ] ) ;
7
+ }
8
+ return p [ x ] ;
9
+ } ;
10
+ for ( let i = 0 ; ; ++ i ) {
11
+ const pa = find ( edges [ i ] [ 0 ] - 1 ) ;
12
+ const pb = find ( edges [ i ] [ 1 ] - 1 ) ;
13
+ if ( pa === pb ) {
14
+ return edges [ i ] ;
15
+ }
16
+ p [ pa ] = pb ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ class UnionFind {
2
+ public:
3
+ UnionFind (int n) {
4
+ p = vector<int >(n);
5
+ size = vector<int >(n, 1 );
6
+ iota (p.begin (), p.end (), 0 );
7
+ }
8
+
9
+ bool unite (int a, int b) {
10
+ int pa = find (a), pb = find (b);
11
+ if (pa == pb) {
12
+ return false ;
13
+ }
14
+ if (size[pa] > size[pb]) {
15
+ p[pb] = pa;
16
+ size[pa] += size[pb];
17
+ } else {
18
+ p[pa] = pb;
19
+ size[pb] += size[pa];
20
+ }
21
+ return true ;
22
+ }
23
+
24
+ int find (int x) {
25
+ if (p[x] != x) {
26
+ p[x] = find (p[x]);
27
+ }
28
+ return p[x];
29
+ }
30
+
31
+ private:
32
+ vector<int > p, size;
33
+ };
34
+
35
+ class Solution {
36
+ public:
37
+ vector<int > findRedundantConnection (vector<vector<int >>& edges) {
38
+ UnionFind uf (edges.size ());
39
+ for (int i = 0 ;; ++i) {
40
+ if (!uf.unite (edges[i][0 ] - 1 , edges[i][1 ] - 1 )) {
41
+ return edges[i];
42
+ }
43
+ }
44
+ }
45
+ };
Original file line number Diff line number Diff line change
1
+ type unionFind struct {
2
+ p , size []int
3
+ }
4
+
5
+ func newUnionFind (n int ) * unionFind {
6
+ p := make ([]int , n )
7
+ size := make ([]int , n )
8
+ for i := range p {
9
+ p [i ] = i
10
+ size [i ] = 1
11
+ }
12
+ return & unionFind {p , size }
13
+ }
14
+
15
+ func (uf * unionFind ) find (x int ) int {
16
+ if uf .p [x ] != x {
17
+ uf .p [x ] = uf .find (uf .p [x ])
18
+ }
19
+ return uf .p [x ]
20
+ }
21
+
22
+ func (uf * unionFind ) union (a , b int ) bool {
23
+ pa , pb := uf .find (a ), uf .find (b )
24
+ if pa == pb {
25
+ return false
26
+ }
27
+ if uf .size [pa ] > uf .size [pb ] {
28
+ uf .p [pb ] = pa
29
+ uf .size [pa ] += uf .size [pb ]
30
+ } else {
31
+ uf .p [pa ] = pb
32
+ uf .size [pb ] += uf .size [pa ]
33
+ }
34
+ return true
35
+ }
36
+
37
+ func findRedundantConnection (edges [][]int ) []int {
38
+ uf := newUnionFind (len (edges ))
39
+ for i := 0 ; ; i ++ {
40
+ if ! uf .union (edges [i ][0 ]- 1 , edges [i ][1 ]- 1 ) {
41
+ return edges [i ]
42
+ }
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ class UnionFind {
2
+ private final int [] p ;
3
+ private final int [] size ;
4
+
5
+ public UnionFind (int n ) {
6
+ p = new int [n ];
7
+ size = new int [n ];
8
+ for (int i = 0 ; i < n ; ++i ) {
9
+ p [i ] = i ;
10
+ size [i ] = 1 ;
11
+ }
12
+ }
13
+
14
+ public int find (int x ) {
15
+ if (p [x ] != x ) {
16
+ p [x ] = find (p [x ]);
17
+ }
18
+ return p [x ];
19
+ }
20
+
21
+ public boolean union (int a , int b ) {
22
+ int pa = find (a ), pb = find (b );
23
+ if (pa == pb ) {
24
+ return false ;
25
+ }
26
+ if (size [pa ] > size [pb ]) {
27
+ p [pb ] = pa ;
28
+ size [pa ] += size [pb ];
29
+ } else {
30
+ p [pa ] = pb ;
31
+ size [pb ] += size [pa ];
32
+ }
33
+ return true ;
34
+ }
35
+ }
36
+
37
+ class Solution {
38
+ public int [] findRedundantConnection (int [][] edges ) {
39
+ UnionFind uf = new UnionFind (edges .length );
40
+ for (int i = 0 ;; ++i ) {
41
+ if (!uf .union (edges [i ][0 ] - 1 , edges [i ][1 ] - 1 )) {
42
+ return edges [i ];
43
+ }
44
+ }
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ class UnionFind {
2
+ constructor ( n ) {
3
+ this . p = Array . from ( { length : n } , ( _ , i ) => i ) ;
4
+ this . size = Array ( n ) . fill ( 1 ) ;
5
+ }
6
+
7
+ find ( x ) {
8
+ if ( this . p [ x ] !== x ) {
9
+ this . p [ x ] = this . find ( this . p [ x ] ) ;
10
+ }
11
+ return this . p [ x ] ;
12
+ }
13
+
14
+ union ( a , b ) {
15
+ const pa = this . find ( a ) ;
16
+ const pb = this . find ( b ) ;
17
+ if ( pa === pb ) {
18
+ return false ;
19
+ }
20
+ if ( this . size [ pa ] > this . size [ pb ] ) {
21
+ this . p [ pb ] = pa ;
22
+ this . size [ pa ] += this . size [ pb ] ;
23
+ } else {
24
+ this . p [ pa ] = pb ;
25
+ this . size [ pb ] += this . size [ pa ] ;
26
+ }
27
+ return true ;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * @param {number[][] } edges
33
+ * @return {number[] }
34
+ */
35
+ var findRedundantConnection = function ( edges ) {
36
+ const uf = new UnionFind ( edges . length ) ;
37
+ for ( let i = 0 ; i < edges . length ; i ++ ) {
38
+ if ( ! uf . union ( edges [ i ] [ 0 ] - 1 , edges [ i ] [ 1 ] - 1 ) ) {
39
+ return edges [ i ] ;
40
+ }
41
+ }
42
+ } ;
Original file line number Diff line number Diff line change
1
+ class UnionFind :
2
+ __slots__ = "p" , "size"
3
+
4
+ def __init__ (self , n : int ):
5
+ self .p : List [int ] = list (range (n ))
6
+ self .size : List [int ] = [1 ] * n
7
+
8
+ def find (self , x : int ) -> int :
9
+ if self .p [x ] != x :
10
+ self .p [x ] = self .find (self .p [x ])
11
+ return self .p [x ]
12
+
13
+ def union (self , a : int , b : int ) -> bool :
14
+ pa , pb = self .find (a ), self .find (b )
15
+ if pa == pb :
16
+ return False
17
+ if self .size [pa ] > self .size [pb ]:
18
+ self .p [pb ] = pa
19
+ self .size [pa ] += self .size [pb ]
20
+ else :
21
+ self .p [pa ] = pb
22
+ self .size [pb ] += self .size [pa ]
23
+ return True
24
+
25
+
26
+ class Solution :
27
+ def findRedundantConnection (self , edges : List [List [int ]]) -> List [int ]:
28
+ uf = UnionFind (len (edges ))
29
+ for a , b in edges :
30
+ if not uf .union (a - 1 , b - 1 ):
31
+ return [a , b ]
You can’t perform that action at this time.
0 commit comments