File tree 1 file changed +37
-0
lines changed
scripts/algorithms/R/Redundant Connection
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 0 ms (Top 100.0%) | Memory: 2.20 MB (Top 35.29%)
2
+
3
+ impl Solution {
4
+ pub fn find_redundant_connection ( edges : Vec < Vec < i32 > > ) -> Vec < i32 > {
5
+ let mut mapper = std:: collections:: HashMap :: new ( ) ;
6
+
7
+ fn parent ( node : i32 , mapper : & mut std:: collections:: HashMap < i32 , i32 > ) -> i32 {
8
+ if !mapper. contains_key ( & node) {
9
+ mapper. insert ( node, node) ;
10
+ }
11
+
12
+ let mut current_node = node;
13
+ while current_node != * mapper. get ( & current_node) . unwrap ( ) {
14
+ current_node = * mapper. get ( & current_node) . unwrap ( ) ;
15
+ }
16
+
17
+ return current_node;
18
+ }
19
+
20
+ for edge in edges {
21
+ let u = edge[ 0 ] ;
22
+ let v = edge[ 1 ] ;
23
+
24
+ let pu = parent ( u, & mut mapper) ;
25
+ let pv = parent ( v, & mut mapper) ;
26
+
27
+ if pu == pv {
28
+ return vec ! [ u, v] ;
29
+ } else {
30
+ mapper. insert ( pv, pu) ;
31
+ }
32
+ }
33
+
34
+ return vec ! [ ] ;
35
+ }
36
+
37
+ }
You can’t perform that action at this time.
0 commit comments