-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution684.cs
42 lines (35 loc) · 899 Bytes
/
Solution684.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
namespace LeetCode.Solutions;
public class Solution684
{
/// <summary>
/// 684. Redundant Connection - Medium
/// <a href="https://leetcode.com/problems/redundant-connection">See the problem</a>
/// </summary>
public int[] FindRedundantConnection(int[][] edges)
{
var parent = new int[edges.Length + 1];
for (var i = 0; i < parent.Length; i++)
{
parent[i] = i;
}
foreach (var edge in edges)
{
var root1 = Find(parent, edge[0]);
var root2 = Find(parent, edge[1]);
if (root1 == root2)
{
return edge;
}
parent[root1] = root2;
}
return [];
}
private static int Find(int[] parent, int x)
{
while (parent[x] != x)
{
x = parent[x];
}
return x;
}
}