Skip to content

Commit 175f9df

Browse files
committed
1584-min-cost-to-connect-all-points-2.md Perfected code.
1 parent 6c22e3a commit 175f9df

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

en/1001-2000/1584-min-cost-to-connect-all-points-2.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ This page, I will only talk about the solution of **Kruskal's Algorithm**.
7373
```python
7474
class Solution:
7575
def __init__(self):
76-
self.parent = []
76+
self.parents = []
7777

7878
def minCostConnectPoints(self, points: List[List[int]]) -> int:
79-
self.parent = list(range(len(points)))
79+
self.parents = list(range(len(points)))
8080
result = 0
8181
edged_points = []
8282

@@ -85,43 +85,49 @@ class Solution:
8585
distance = abs(point[0] - points[j][0]) + \
8686
abs(point[1] - points[j][1])
8787
heapq.heappush(edged_points, (distance, i, j))
88-
88+
8989
while edged_points:
9090
distance, i, j = heapq.heappop(edged_points)
9191

92-
if self.same_root(i, j):
92+
if self.is_same_root(i, j):
9393
continue
9494

9595
self.unite(i, j)
9696

9797
result += distance
98-
98+
9999
return result
100100

101101
def unite(self, x, y):
102102
root_x = self.find_root(x)
103103
root_y = self.find_root(y)
104-
self.parent[root_y] = root_x
104+
105+
self.parents[root_y] = root_x
105106

106107
def find_root(self, x):
107-
if x == self.parent[x]:
108+
parent = self.parents[x]
109+
110+
if x == parent:
108111
return x
109112

110-
self.parent[x] = self.find_root(self.parent[x])
113+
root = self.find_root(parent)
114+
115+
self.parents[x] = root
111116

112-
return self.parent[x]
117+
return root
113118

114-
def same_root(self, x, y):
119+
def is_same_root(self, x, y):
115120
return self.find_root(x) == self.find_root(y)
116121
```
117122

118123
## Java
119124
```java
125+
// Welcome to create a PR to complete the code of this language, thanks!
120126
```
121127

122128
## Python
123129
```python
124-
// Welcome to create a PR to complete the code of this language, thanks!
130+
# Welcome to create a PR to complete the code of this language, thanks!
125131
```
126132

127133
## C++

zh/1001-2000/1584-min-cost-to-connect-all-points-2.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ This page, I will only talk about the solution of **Kruskal's Algorithm**.
7373
```python
7474
class Solution:
7575
def __init__(self):
76-
self.parent = []
76+
self.parents = []
7777

7878
def minCostConnectPoints(self, points: List[List[int]]) -> int:
79-
self.parent = list(range(len(points)))
79+
self.parents = list(range(len(points)))
8080
result = 0
8181
edged_points = []
8282

@@ -85,43 +85,49 @@ class Solution:
8585
distance = abs(point[0] - points[j][0]) + \
8686
abs(point[1] - points[j][1])
8787
heapq.heappush(edged_points, (distance, i, j))
88-
88+
8989
while edged_points:
9090
distance, i, j = heapq.heappop(edged_points)
9191

92-
if self.same_root(i, j):
92+
if self.is_same_root(i, j):
9393
continue
9494

9595
self.unite(i, j)
9696

9797
result += distance
98-
98+
9999
return result
100100

101101
def unite(self, x, y):
102102
root_x = self.find_root(x)
103103
root_y = self.find_root(y)
104-
self.parent[root_y] = root_x
104+
105+
self.parents[root_y] = root_x
105106

106107
def find_root(self, x):
107-
if x == self.parent[x]:
108+
parent = self.parents[x]
109+
110+
if x == parent:
108111
return x
109112

110-
self.parent[x] = self.find_root(self.parent[x])
113+
root = self.find_root(parent)
114+
115+
self.parents[x] = root
111116

112-
return self.parent[x]
117+
return root
113118

114-
def same_root(self, x, y):
119+
def is_same_root(self, x, y):
115120
return self.find_root(x) == self.find_root(y)
116121
```
117122

118123
## Java
119124
```java
125+
// Welcome to create a PR to complete the code of this language, thanks!
120126
```
121127

122128
## Python
123129
```python
124-
// Welcome to create a PR to complete the code of this language, thanks!
130+
# Welcome to create a PR to complete the code of this language, thanks!
125131
```
126132

127133
## C++

0 commit comments

Comments
 (0)