Skip to content

Commit 51f7c43

Browse files
committed
Runtime: 6190 ms (Top 5.05%) | Memory: 57.3 MB (Top 48.67%)
1 parent 17f8376 commit 51f7c43

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
1+
# Runtime: 6190 ms (Top 5.05%) | Memory: 57.3 MB (Top 48.67%)
12
class Solution:
23
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
3-
4+
45
class UnionFind:
56
def __init__(self):
67
self.parents = {}
78
self.ranks = {}
8-
9+
910
def insert(self, x):
1011
if x not in self.parents:
1112
self.parents[x] = x
1213
self.ranks[x] = 0
13-
14+
1415
def find_parent(self, x):
1516
if self.parents[x] != x:
1617
self.parents[x] = self.find_parent(self.parents[x])
1718
return self.parents[x]
18-
19+
1920
def union(self, x, y):
2021
self.insert(x)
2122
self.insert(y)
2223
x, y = self.find_parent(x), self.find_parent(y)
2324
if x == y:
24-
return
25+
return
2526
if self.ranks[x] > self.ranks[y]:
2627
self.parents[y] = x
2728
else:
2829
self.parents[x] = y
2930
if self.ranks[x] == self.ranks[y]:
3031
self.ranks[y] += 1
31-
32+
3233
time2meets = defaultdict(list)
3334
for x, y, t in meetings:
3435
time2meets[t].append((x, y))
3536
time2meets = sorted(time2meets.items())
36-
37+
3738
curr_know = set([0, firstPerson])
3839

3940
for time, meets in time2meets:
4041
uf = UnionFind()
4142
for x, y in meets:
4243
uf.union(x, y)
43-
44+
4445
groups = defaultdict(set)
4546
for idx in uf.parents:
4647
groups[uf.find_parent(idx)].add(idx)
47-
48+
4849
for group in groups.values():
4950
if group & curr_know:
5051
curr_know.update(group)
5152

52-
return list(curr_know)
53+
return list(curr_know)

0 commit comments

Comments
 (0)