Skip to content

Commit c48f444

Browse files
committed
Runtime: 772 ms (Top 55.10%) | Memory: 34.3 MB (Top 72.08%)
1 parent 7045a01 commit c48f444

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
# Runtime: 772 ms (Top 55.10%) | Memory: 34.3 MB (Top 72.08%)
12
class Solution(object):
23
def __init__(self):
34
self.parents = []
45
self.count = []
5-
6+
67
def makeConnected(self, n, connections):
78
"""
89
:type n: int
910
:type connections: List[List[int]]
1011
:rtype: int
1112
"""
1213
if len(connections) < n-1:
13-
return -1
14+
return -1
1415
self.parents = [i for i in range(n)]
1516
self.count = [1 for _ in range(n)]
1617
for connection in connections:
1718
a, b = connection[0], connection[1]
1819
self.union(a, b)
1920
return len({self.find(i) for i in range(n)}) - 1
20-
21+
2122
def find(self, node):
2223
"""
2324
:type node: int
@@ -26,7 +27,7 @@ def find(self, node):
2627
while(node != self.parents[node]):
2728
node = self.parents[node];
2829
return node
29-
30+
3031
def union(self, a, b):
3132
"""
3233
:type a: int
@@ -35,11 +36,11 @@ def union(self, a, b):
3536
"""
3637
a_parent, b_parent = self.find(a), self.find(b)
3738
a_size, b_size = self.count[a_parent], self.count[b_parent]
38-
39+
3940
if a_parent != b_parent:
4041
if a_size < b_size:
4142
self.parents[a_parent] = b_parent
4243
self.count[b_parent] += a_size
4344
else:
4445
self.parents[b_parent] = a_parent
45-
self.count[a_parent] += b_size
46+
self.count[a_parent] += b_size

0 commit comments

Comments
 (0)