Skip to content

Commit 5d88a1a

Browse files
committed
feat: update solutions to lc problems
* No.1135.Connecting Cities With Minimum Cost * No.1584.Min Cost to Connect All Points
1 parent 14652cd commit 5d88a1a

File tree

13 files changed

+405
-487
lines changed

13 files changed

+405
-487
lines changed

solution/1100-1199/1135.Connecting Cities With Minimum Cost/README.md

+53-83
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
最小生成树问题。
56+
最小生成树问题。设 n 表示点数,m 表示边数。
5757

5858
**方法一:Kruskal 算法**
5959

60+
时间复杂度 O(mlogm)。
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**
@@ -66,28 +68,23 @@
6668
```python
6769
class Solution:
6870
def minimumCost(self, n: int, connections: List[List[int]]) -> int:
69-
p = list(range(n))
70-
connections.sort(key=lambda x: x[2])
71-
res = 0
72-
7371
def find(x):
7472
if p[x] != x:
7573
p[x] = find(p[x])
7674
return p[x]
7775

78-
def union(a, b):
79-
pa, pb = find(a - 1), find(b - 1)
80-
if pa == pb:
81-
return False
82-
p[pa] = pb
83-
return True
84-
85-
for c1, c2, cost in connections:
86-
if union(c1, c2):
87-
n -= 1
88-
res += cost
89-
if n == 1:
90-
return res
76+
connections.sort(key=lambda x: x[2])
77+
p = list(range(n))
78+
ans = 0
79+
for x, y, cost in connections:
80+
x, y = x - 1, y - 1
81+
if find(x) == find(y):
82+
continue
83+
p[find(x)] = find(y)
84+
ans += cost
85+
n -= 1
86+
if n == 1:
87+
return ans
9188
return -1
9289
```
9390

@@ -100,19 +97,21 @@ class Solution {
10097
private int[] p;
10198

10299
public int minimumCost(int n, int[][] connections) {
100+
Arrays.sort(connections, Comparator.comparingInt(a -> a[2]));
103101
p = new int[n];
104102
for (int i = 0; i < n; ++i) {
105103
p[i] = i;
106104
}
107-
Arrays.sort(connections, (a, b) -> a[2] - b[2]);
108-
int res = 0;
105+
int ans = 0;
109106
for (int[] e : connections) {
110-
if (union(e[0], e[1])) {
111-
res += e[2];
112-
--n;
113-
if (n == 1) {
114-
return res;
115-
}
107+
int x = e[0] - 1, y = e[1] - 1, cost = e[2];
108+
if (find(x) == find(y)) {
109+
continue;
110+
}
111+
p[find(x)] = find(y);
112+
ans += cost;
113+
if (--n == 1) {
114+
return ans;
116115
}
117116
}
118117
return -1;
@@ -124,15 +123,6 @@ class Solution {
124123
}
125124
return p[x];
126125
}
127-
128-
private boolean union(int a, int b) {
129-
int pa = find(a - 1), pb = find(b - 1);
130-
if (pa == pb) {
131-
return false;
132-
}
133-
p[pa] = pb;
134-
return true;
135-
}
136126
}
137127
```
138128

@@ -143,23 +133,18 @@ class Solution {
143133
public:
144134
vector<int> p;
145135

146-
int minimumCost(int n, vector<vector<int>> &connections) {
136+
int minimumCost(int n, vector<vector<int>>& connections) {
147137
p.resize(n);
148138
for (int i = 0; i < n; ++i) p[i] = i;
149-
auto cmp = [](auto &a, auto &b)
150-
{
151-
return a[2] < b[2];
152-
};
153-
sort(connections.begin(), connections.end(), cmp);
154-
int res = 0;
155-
for (auto e : connections)
139+
sort(connections.begin(), connections.end(), [](auto& a, auto& b) {return a[2] < b[2];});
140+
int ans = 0;
141+
for (auto& e : connections)
156142
{
157-
if (unite(e[0], e[1]))
158-
{
159-
res += e[2];
160-
--n;
161-
if (n == 1) return res;
162-
}
143+
int x = e[0] - 1, y = e[1] - 1, cost = e[2];
144+
if (find(x) == find(y)) continue;
145+
p[find(x)] = find(y);
146+
ans += cost;
147+
if (--n == 1) return ans;
163148
}
164149
return -1;
165150
}
@@ -168,57 +153,42 @@ public:
168153
if (p[x] != x) p[x] = find(p[x]);
169154
return p[x];
170155
}
171-
172-
bool unite(int a, int b) {
173-
int pa = find(a - 1), pb = find(b - 1);
174-
if (pa == pb) return false;
175-
p[pa] = pb;
176-
return true;
177-
}
178156
};
179157
```
180158
181159
### **Go**
182160
183161
```go
184-
var p []int
185-
186162
func minimumCost(n int, connections [][]int) int {
187-
p = make([]int, n)
188-
for i := 0; i < len(p); i++ {
163+
p := make([]int, n)
164+
for i := range p {
189165
p[i] = i
190166
}
191167
sort.Slice(connections, func(i, j int) bool {
192168
return connections[i][2] < connections[j][2]
193169
})
194-
res := 0
170+
var find func(x int) int
171+
find = func(x int) int {
172+
if p[x] != x {
173+
p[x] = find(p[x])
174+
}
175+
return p[x]
176+
}
177+
ans := 0
195178
for _, e := range connections {
196-
if union(e[0], e[1]) {
197-
res += e[2]
198-
n--
199-
if n == 1 {
200-
return res
201-
}
179+
x, y, cost := e[0]-1, e[1]-1, e[2]
180+
if find(x) == find(y) {
181+
continue
182+
}
183+
p[find(x)] = find(y)
184+
ans += cost
185+
n--
186+
if n == 1 {
187+
return ans
202188
}
203189
}
204190
return -1
205191
}
206-
207-
func find(x int) int {
208-
if p[x] != x {
209-
p[x] = find(p[x])
210-
}
211-
return p[x]
212-
}
213-
214-
func union(a, b int) bool {
215-
pa, pb := find(a-1), find(b-1)
216-
if pa == pb {
217-
return false
218-
}
219-
p[pa] = pb
220-
return true
221-
}
222192
```
223193

224194
### **...**

0 commit comments

Comments
 (0)