diff --git a/swift/0684-redundant-connection.swift b/swift/0684-redundant-connection.swift new file mode 100644 index 000000000..3f243ca8d --- /dev/null +++ b/swift/0684-redundant-connection.swift @@ -0,0 +1,54 @@ +/** + * Question Link: https://leetcode.com/problems/redundant-connection/ + */ + + class UnionFind { + var parents = [Int: Int]() + var ranks = [Int: Int]() + + init(n: Int) { + for i in 1.. Int { + var p = parents[n]! + while p != parents[p]! { + parents[p] = parents[parents[p]!] + p = parents[p]! + } + return p + } + + func union(n1: Int, n2: Int) -> Bool { + let p1 = find(n: n1) + let p2 = find(n: n2) + if p1 == p2 { + return false + } + + if ranks[p1]! > ranks[p2]! { + parents[p2] = p1 + } else if ranks[p1]! < ranks[p2]! { + parents[p1] = p2 + } else { + parents[p1] = p2 + ranks[p2, default: 0] += 1 + } + return true + } +} + +class Solution { + func findRedundantConnection(_ edges: [[Int]]) -> [Int] { + let unionFind = UnionFind(n: edges.count) + for edge in edges { + if !unionFind.union(n1: edge[0], n2: edge[1]) { + return [edge[0], edge[1]] + } + } + return [] + } +} \ No newline at end of file