Skip to content

Commit d54faaf

Browse files
committed
Jan 29
1 parent 4a8c954 commit d54faaf

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

2025-01-January-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
| January 26 | [2127. Maximum Employees to Be Invited to a Meeting](https://leetcode.com/problems/maximum-employees-to-be-invited-to-a-meeting/) | Hard | Unsolved |
3232
| January 27 | [1462. Course Schedule IV](https://leetcode.com/problems/course-schedule-iv/) | Medium | Solved |
3333
| January 28 | [2658. Maximum Number of Fish in a Grid](https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/) | Medium | Solved |
34-
| January 29 | []() | | |
34+
| January 29 | [684. Redundant Connection](https://leetcode.com/problems/redundant-connection/) | Medium | Solved |
3535
| January 30 | []() | | |
3636
| January 31 | []() | | |
3737

@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 4 | 4 | 0 |
43-
| Medium | 20 | 17 | 3 |
43+
| Medium | 21 | 18 | 3 |
4444
| Hard | 3 | 0 | 3 |
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import List
2+
3+
4+
class UnionFind:
5+
def __init__(self, n: int):
6+
self.parent = list(range(n+1))
7+
8+
def find(self, x: int) -> int:
9+
if x != self.parent[x]:
10+
self.parent[x] = self.find(self.parent[x])
11+
return self.parent[x]
12+
13+
def union(self, x: int, y: int) -> None:
14+
self.parent[self.find(x)] = self.find(y)
15+
16+
17+
class Solution:
18+
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
19+
uf = UnionFind(len(edges))
20+
for u, v in edges:
21+
if uf.find(u) == uf.find(v):
22+
return [u, v]
23+
else:
24+
uf.union(u, v)
25+
return None
26+
27+
28+
def main():
29+
edges = [[1, 2], [1, 3], [2, 3]]
30+
assert Solution().findRedundantConnection(edges) == [2, 3]
31+
32+
edges = [[1, 2], [2, 3], [3, 4], [1, 4], [1, 5]]
33+
assert Solution().findRedundantConnection(edges) == [1, 4]
34+
35+
36+
if __name__ == '__main__':
37+
main()

0 commit comments

Comments
 (0)