File tree 1 file changed +7
-6
lines changed
scripts/algorithms/R/Redundant Connection
1 file changed +7
-6
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 1 ms (Top 89.63%) | Memory: 43.5 MB (Top 75.35%)
1
2
class Solution {
2
3
public int [] findRedundantConnection (int [][] edges ) {
3
4
UnionFind uf = new UnionFind (edges .length );
@@ -8,11 +9,11 @@ public int[] findRedundantConnection(int[][] edges) {
8
9
}
9
10
return null ;
10
11
}
11
-
12
+
12
13
private class UnionFind {
13
14
int [] rank ;
14
15
int [] root ;
15
-
16
+
16
17
UnionFind (int n ) {
17
18
rank = new int [n + 1 ];
18
19
root = new int [n + 1 ];
@@ -21,14 +22,14 @@ private class UnionFind {
21
22
rank [i ] = 1 ;
22
23
}
23
24
}
24
-
25
+
25
26
int find (int x ) {
26
27
if (x == root [x ]) {
27
28
return x ;
28
29
}
29
30
return root [x ] = find (root [x ]);
30
31
}
31
-
32
+
32
33
boolean union (int x , int y ) {
33
34
int rootX = find (x );
34
35
int rootY = find (y );
@@ -39,11 +40,11 @@ boolean union(int x, int y) {
39
40
root [rootX ] = root [rootY ];
40
41
} else {
41
42
root [rootY ] = root [rootX ];
42
- rank [rootX ]++;
43
+ rank [rootX ]++;
43
44
}
44
45
return true ;
45
46
}
46
47
return false ;
47
48
}
48
49
}
49
- }
50
+ }
You can’t perform that action at this time.
0 commit comments