Skip to content

Commit a456ec9

Browse files
committed
feat: add solutions to lc problem: No.1584.Min Cost to Connect All
Points
1 parent 37b593b commit a456ec9

File tree

7 files changed

+513
-40
lines changed

7 files changed

+513
-40
lines changed

lcof2/剑指 Offer II 004. 只出现一次的数字/Solution.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
class Solution
32
{
43
public:

solution/1500-1599/1584.Min Cost to Connect All Points/README.md

Lines changed: 217 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,242 @@
6565
<li>所有点&nbsp;<code>(x<sub>i</sub>, y<sub>i</sub>)</code>&nbsp;两两不同。</li>
6666
</ul>
6767

68-
6968
## 解法
7069

7170
<!-- 这里可写通用的实现逻辑 -->
7271

72+
最小生成树 + 并查集。先将所有边按照长度由小到大进行排序,循环遍历每条边,逐个加入到图中,当所有点达到一个连通状态时,退出循环,返回此时的总费用即可。
73+
74+
模板 1——朴素并查集:
75+
76+
```python
77+
# 初始化,p存储每个点的父节点
78+
p = list(range(n))
79+
80+
# 返回x的祖宗节点
81+
def find(x):
82+
if p[x] != x:
83+
# 路径压缩
84+
p[x] = find(p[x])
85+
return p[x]
86+
87+
# 合并a和b所在的两个集合
88+
p[find(a)] = find(b)
89+
```
90+
91+
模板 2——维护 size 的并查集:
92+
93+
```python
94+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
95+
p = list(range(n))
96+
size = [1] * n
97+
98+
# 返回x的祖宗节点
99+
def find(x):
100+
if p[x] != x:
101+
# 路径压缩
102+
p[x] = find(p[x])
103+
return p[x]
104+
105+
# 合并a和b所在的两个集合
106+
if find(a) != find(b):
107+
size[find(b)] += size[find(a)]
108+
p[find(a)] = find(b)
109+
```
110+
111+
模板 3——维护到祖宗节点距离的并查集:
112+
113+
```python
114+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
115+
p = list(range(n))
116+
d = [0] * n
117+
118+
# 返回x的祖宗节点
119+
def find(x):
120+
if p[x] != x:
121+
t = find(p[x])
122+
d[x] += d[p[x]]
123+
p[x] = t
124+
return p[x]
125+
126+
# 合并a和b所在的两个集合
127+
p[find(a)] = find(b)
128+
d[find(a)] = distance
129+
```
130+
73131
<!-- tabs:start -->
74132

75133
### **Python3**
76134

77135
<!-- 这里可写当前语言的特殊实现逻辑 -->
78136

79137
```python
80-
138+
class Solution:
139+
def minCostConnectPoints(self, points: List[List[int]]) -> int:
140+
n = len(points)
141+
p = list(range(n))
142+
143+
def find(x):
144+
if p[x] != x:
145+
p[x] = find(p[x])
146+
return p[x]
147+
148+
edges = []
149+
for i in range(n):
150+
x1, y1 = points[i]
151+
for j in range(i + 1, n):
152+
x2, y2 = points[j]
153+
edges.append([abs(x1 - x2) + abs(y1 - y2), i, j])
154+
edges.sort()
155+
res = 0
156+
for cost, i, j in edges:
157+
if find(i) == find(j):
158+
continue
159+
p[find(i)] = find(j)
160+
n -= 1
161+
res += cost
162+
if n == 1:
163+
return res
164+
return 0
81165
```
82166

83167
### **Java**
84168

85169
<!-- 这里可写当前语言的特殊实现逻辑 -->
86170

87171
```java
172+
class Solution {
173+
private int[] p;
174+
public int minCostConnectPoints(int[][] points) {
175+
int n = points.length;
176+
p = new int[n];
177+
for (int i = 0; i < n; ++i) {
178+
p[i] = i;
179+
}
180+
List<int[]> edges = new ArrayList<>();
181+
for (int i = 0; i < n; ++i) {
182+
int x1 = points[i][0], y1 = points[i][1];
183+
for (int j = i + 1; j < n; ++j) {
184+
int x2 = points[j][0], y2 = points[j][1];
185+
edges.add(new int[]{Math.abs(x1 - x2) + Math.abs(y1 - y2), i, j});
186+
}
187+
}
188+
edges.sort(Comparator.comparingInt(a -> a[0]));
189+
int res = 0;
190+
for (int[] e : edges) {
191+
if (find(e[1]) == find(e[2])) {
192+
continue;
193+
}
194+
p[find(e[1])] = find(e[2]);
195+
--n;
196+
res += e[0];
197+
if (n == 1) {
198+
break;
199+
}
200+
}
201+
return res;
202+
}
203+
204+
private int find(int x) {
205+
if (p[x] != x) {
206+
p[x] = find(p[x]);
207+
}
208+
return p[x];
209+
}
210+
}
211+
```
212+
213+
### **C++**
214+
215+
```cpp
216+
class Solution {
217+
public:
218+
vector<int> p;
219+
220+
int minCostConnectPoints(vector<vector<int>>& points) {
221+
int n = points.size();
222+
p.resize(n);
223+
for (int i = 0; i < n; ++i) p[i] = i;
224+
vector<vector<int>> edges;
225+
for (int i = 0; i < n; ++i)
226+
{
227+
int x1 = points[i][0], y1 = points[i][1];
228+
for (int j = i + 1; j < n; ++j)
229+
{
230+
int x2 = points[j][0], y2 = points[j][1];
231+
edges.push_back({abs(x1 - x2) + abs(y1 - y2), i, j});
232+
}
233+
}
234+
sort(edges.begin(), edges.end());
235+
int res = 0;
236+
for (auto e : edges)
237+
{
238+
if (find(e[1]) == find(e[2])) continue;
239+
p[find(e[1])] = find(e[2]);
240+
--n;
241+
res += e[0];
242+
if (n == 1) break;
243+
}
244+
return res;
245+
}
246+
247+
int find(int x) {
248+
if (p[x] != x) p[x] = find(p[x]);
249+
return p[x];
250+
}
251+
};
252+
```
88253
254+
### **Go**
255+
256+
```go
257+
var p []int
258+
259+
func minCostConnectPoints(points [][]int) int {
260+
n := len(points)
261+
p = make([]int, n)
262+
for i := 0; i < n; i++ {
263+
p[i] = i
264+
}
265+
var edges [][]int
266+
for i := 0; i < n; i++ {
267+
x1, y1 := points[i][0], points[i][1]
268+
for j := i + 1; j < n; j++ {
269+
x2, y2 := points[j][0], points[j][1]
270+
edges = append(edges, []int{abs(x1 - x2) + abs(y1 - y2), i, j})
271+
}
272+
}
273+
sort.Slice(edges, func(i, j int) bool {
274+
return edges[i][0] < edges[j][0]
275+
})
276+
res := 0
277+
for _, e := range edges {
278+
if find(e[1]) == find(e[2]) {
279+
continue
280+
}
281+
p[find(e[1])] = find(e[2])
282+
n--
283+
res += e[0]
284+
if n == 1 {
285+
break
286+
}
287+
}
288+
return res
289+
}
290+
291+
func find(x int) int {
292+
if p[x] != x {
293+
p[x] = find(p[x])
294+
}
295+
return p[x]
296+
}
297+
298+
func abs(x int) int {
299+
if x > 0 {
300+
return x
301+
}
302+
return -x
303+
}
89304
```
90305

91306
### **...**

0 commit comments

Comments
 (0)