Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1064 from notauserx/684-Redundant-Conn…
Browse files Browse the repository at this point in the history
…ection.cs

Create 684-Redundant-Connection.cs
  • Loading branch information
Ahmad-A0 authored Sep 11, 2022
2 parents e3f5d4a + eb0f37d commit 29f51b7
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions csharp/684-Redundant-Connection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public class Solution {
public int[] FindRedundantConnection(int[][] edges) {
var nodes = edges.Length;
int[] parent = new int[nodes + 1];
int[] rank = new int[nodes + 1];

for(int i = 0; i < nodes; i++) {
parent[i] = i;
rank[i] = 1;
}

int findParent (int n) {
var p = parent[n];

while(p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}

return p;
}
bool union(int n1, int n2) {
(int p1, int p2) = (findParent(n1), findParent(n2));

if(p1 == p2) return false;

if(rank[p1] > rank[p2]) {
parent[p2] = p1;
rank[p1] += rank[p2];
} else {
parent[p1] = p2;
rank[p2] += rank[p1];
}

return true;
}

foreach(var edge in edges) {
if(union(edge[0], edge[1]) is false)
return edge;
}

return new int[2];
}
}

0 comments on commit 29f51b7

Please sign in to comment.