Skip to content

Commit 6f22750

Browse files
committed
feat: add solutions to lc problem: No.1135
No.1135.Connecting Cities With Minimum Cost
1 parent 73d175d commit 6f22750

File tree

6 files changed

+148
-59
lines changed

6 files changed

+148
-59
lines changed

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

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@
5353

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

56-
最小生成树问题。设 n 表示点数,m 表示边数。
57-
5856
**方法一:Kruskal 算法**
5957

60-
时间复杂度 O(mlogm)。
58+
Kruskal 算法是一种贪心算法,用于计算最小生成树。
59+
60+
Kruskal 算法的基本思想是,每次从边集中选择一条最小的边,如果这条边连接的两个顶点不在同一个连通分量中,则将这条边加入到最小生成树中,否则舍弃这条边。
61+
62+
对于本题,我们可以将边按照连通成本从小到大排序,用并查集维护连通分量,每次选择一条最小的边,如果这条边连接的两个顶点不在同一个连通分量中,则合并这两个顶点,然后累加连通成本。如果出现连通份量为 $1$ 的情况,则说明所有顶点都连通了,返回累加的连通成本,否则返回 $-1$。
63+
64+
时间复杂度 $O(m \times \log m)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别为边数和顶点数。
6165

6266
<!-- tabs:start -->
6367

@@ -131,49 +135,49 @@ class Solution {
131135
```cpp
132136
class Solution {
133137
public:
134-
vector<int> p;
135-
136138
int minimumCost(int n, vector<vector<int>>& connections) {
137-
p.resize(n);
138-
for (int i = 0; i < n; ++i) p[i] = i;
139+
vector<int> p(n);
140+
iota(p.begin(), p.end(), 0);
139141
sort(connections.begin(), connections.end(), [](auto& a, auto& b) { return a[2] < b[2]; });
140142
int ans = 0;
143+
function<int(int)> find = [&](int x) -> int {
144+
if (p[x] != x) {
145+
p[x] = find(p[x]);
146+
}
147+
return p[x];
148+
};
141149
for (auto& e : connections) {
142150
int x = e[0] - 1, y = e[1] - 1, cost = e[2];
143-
if (find(x) == find(y)) continue;
151+
if (find(x) == find(y)) {
152+
continue;
153+
}
144154
p[find(x)] = find(y);
145155
ans += cost;
146-
if (--n == 1) return ans;
156+
if (--n == 1) {
157+
return ans;
158+
}
147159
}
148160
return -1;
149161
}
150-
151-
int find(int x) {
152-
if (p[x] != x) p[x] = find(p[x]);
153-
return p[x];
154-
}
155162
};
156163
```
157164
158165
### **Go**
159166
160167
```go
161-
func minimumCost(n int, connections [][]int) int {
168+
func minimumCost(n int, connections [][]int) (ans int) {
162169
p := make([]int, n)
163170
for i := range p {
164171
p[i] = i
165172
}
166-
sort.Slice(connections, func(i, j int) bool {
167-
return connections[i][2] < connections[j][2]
168-
})
169-
var find func(x int) int
173+
sort.Slice(connections, func(i, j int) bool { return connections[i][2] < connections[j][2] })
174+
var find func(int) int
170175
find = func(x int) int {
171176
if p[x] != x {
172177
p[x] = find(p[x])
173178
}
174179
return p[x]
175180
}
176-
ans := 0
177181
for _, e := range connections {
178182
x, y, cost := e[0]-1, e[1]-1, e[2]
179183
if find(x) == find(y) {
@@ -183,13 +187,43 @@ func minimumCost(n int, connections [][]int) int {
183187
ans += cost
184188
n--
185189
if n == 1 {
186-
return ans
190+
return
187191
}
188192
}
189193
return -1
190194
}
191195
```
192196

197+
### **TypeScript**
198+
199+
```ts
200+
function minimumCost(n: number, connections: number[][]): number {
201+
const p = new Array(n);
202+
for (let i = 0; i < n; ++i) {
203+
p[i] = i;
204+
}
205+
const find = (x: number): number => {
206+
if (p[x] !== x) {
207+
p[x] = find(p[x]);
208+
}
209+
return p[x];
210+
};
211+
connections.sort((a, b) => a[2] - b[2]);
212+
let ans = 0;
213+
for (const [x, y, cost] of connections) {
214+
if (find(x - 1) == find(y - 1)) {
215+
continue;
216+
}
217+
p[find(x - 1)] = find(y - 1);
218+
ans += cost;
219+
if (--n == 1) {
220+
return ans;
221+
}
222+
}
223+
return -1;
224+
}
225+
```
226+
193227
### **...**
194228

195229
```

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

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,49 +109,49 @@ class Solution {
109109
```cpp
110110
class Solution {
111111
public:
112-
vector<int> p;
113-
114112
int minimumCost(int n, vector<vector<int>>& connections) {
115-
p.resize(n);
116-
for (int i = 0; i < n; ++i) p[i] = i;
113+
vector<int> p(n);
114+
iota(p.begin(), p.end(), 0);
117115
sort(connections.begin(), connections.end(), [](auto& a, auto& b) { return a[2] < b[2]; });
118116
int ans = 0;
117+
function<int(int)> find = [&](int x) -> int {
118+
if (p[x] != x) {
119+
p[x] = find(p[x]);
120+
}
121+
return p[x];
122+
};
119123
for (auto& e : connections) {
120124
int x = e[0] - 1, y = e[1] - 1, cost = e[2];
121-
if (find(x) == find(y)) continue;
125+
if (find(x) == find(y)) {
126+
continue;
127+
}
122128
p[find(x)] = find(y);
123129
ans += cost;
124-
if (--n == 1) return ans;
130+
if (--n == 1) {
131+
return ans;
132+
}
125133
}
126134
return -1;
127135
}
128-
129-
int find(int x) {
130-
if (p[x] != x) p[x] = find(p[x]);
131-
return p[x];
132-
}
133136
};
134137
```
135138
136139
### **Go**
137140
138141
```go
139-
func minimumCost(n int, connections [][]int) int {
142+
func minimumCost(n int, connections [][]int) (ans int) {
140143
p := make([]int, n)
141144
for i := range p {
142145
p[i] = i
143146
}
144-
sort.Slice(connections, func(i, j int) bool {
145-
return connections[i][2] < connections[j][2]
146-
})
147-
var find func(x int) int
147+
sort.Slice(connections, func(i, j int) bool { return connections[i][2] < connections[j][2] })
148+
var find func(int) int
148149
find = func(x int) int {
149150
if p[x] != x {
150151
p[x] = find(p[x])
151152
}
152153
return p[x]
153154
}
154-
ans := 0
155155
for _, e := range connections {
156156
x, y, cost := e[0]-1, e[1]-1, e[2]
157157
if find(x) == find(y) {
@@ -161,13 +161,43 @@ func minimumCost(n int, connections [][]int) int {
161161
ans += cost
162162
n--
163163
if n == 1 {
164-
return ans
164+
return
165165
}
166166
}
167167
return -1
168168
}
169169
```
170170

171+
### **TypeScript**
172+
173+
```ts
174+
function minimumCost(n: number, connections: number[][]): number {
175+
const p = new Array(n);
176+
for (let i = 0; i < n; ++i) {
177+
p[i] = i;
178+
}
179+
const find = (x: number): number => {
180+
if (p[x] !== x) {
181+
p[x] = find(p[x]);
182+
}
183+
return p[x];
184+
};
185+
connections.sort((a, b) => a[2] - b[2]);
186+
let ans = 0;
187+
for (const [x, y, cost] of connections) {
188+
if (find(x - 1) == find(y - 1)) {
189+
continue;
190+
}
191+
p[find(x - 1)] = find(y - 1);
192+
ans += cost;
193+
if (--n == 1) {
194+
return ans;
195+
}
196+
}
197+
return -1;
198+
}
199+
```
200+
171201
### **...**
172202

173203
```
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
class Solution {
22
public:
3-
vector<int> p;
4-
53
int minimumCost(int n, vector<vector<int>>& connections) {
6-
p.resize(n);
7-
for (int i = 0; i < n; ++i) p[i] = i;
4+
vector<int> p(n);
5+
iota(p.begin(), p.end(), 0);
86
sort(connections.begin(), connections.end(), [](auto& a, auto& b) { return a[2] < b[2]; });
97
int ans = 0;
8+
function<int(int)> find = [&](int x) -> int {
9+
if (p[x] != x) {
10+
p[x] = find(p[x]);
11+
}
12+
return p[x];
13+
};
1014
for (auto& e : connections) {
1115
int x = e[0] - 1, y = e[1] - 1, cost = e[2];
12-
if (find(x) == find(y)) continue;
16+
if (find(x) == find(y)) {
17+
continue;
18+
}
1319
p[find(x)] = find(y);
1420
ans += cost;
15-
if (--n == 1) return ans;
21+
if (--n == 1) {
22+
return ans;
23+
}
1624
}
1725
return -1;
1826
}
19-
20-
int find(int x) {
21-
if (p[x] != x) p[x] = find(p[x]);
22-
return p[x];
23-
}
2427
};

solution/1100-1199/1135.Connecting Cities With Minimum Cost/Solution.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
func minimumCost(n int, connections [][]int) int {
1+
func minimumCost(n int, connections [][]int) (ans int) {
22
p := make([]int, n)
33
for i := range p {
44
p[i] = i
55
}
6-
sort.Slice(connections, func(i, j int) bool {
7-
return connections[i][2] < connections[j][2]
8-
})
9-
var find func(x int) int
6+
sort.Slice(connections, func(i, j int) bool { return connections[i][2] < connections[j][2] })
7+
var find func(int) int
108
find = func(x int) int {
119
if p[x] != x {
1210
p[x] = find(p[x])
1311
}
1412
return p[x]
1513
}
16-
ans := 0
1714
for _, e := range connections {
1815
x, y, cost := e[0]-1, e[1]-1, e[2]
1916
if find(x) == find(y) {
@@ -23,7 +20,7 @@ func minimumCost(n int, connections [][]int) int {
2320
ans += cost
2421
n--
2522
if n == 1 {
26-
return ans
23+
return
2724
}
2825
}
2926
return -1
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function minimumCost(n: number, connections: number[][]): number {
2+
const p = new Array(n);
3+
for (let i = 0; i < n; ++i) {
4+
p[i] = i;
5+
}
6+
const find = (x: number): number => {
7+
if (p[x] !== x) {
8+
p[x] = find(p[x]);
9+
}
10+
return p[x];
11+
};
12+
connections.sort((a, b) => a[2] - b[2]);
13+
let ans = 0;
14+
for (const [x, y, cost] of connections) {
15+
if (find(x - 1) == find(y - 1)) {
16+
continue;
17+
}
18+
p[find(x - 1)] = find(y - 1);
19+
ans += cost;
20+
if (--n == 1) {
21+
return ans;
22+
}
23+
}
24+
return -1;
25+
}

solution/1800-1899/1886.Determine Whether Matrix Can Be Obtained By Rotation/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
方法一:模拟旋转
52+
**方法一:模拟旋转**
5353

5454
旋转矩阵,判断矩阵是否一致,旋转方式同 [48. 旋转图像](https://leetcode.cn/problems/rotate-image/)
5555

56-
方法二:原地比较
56+
**方法二:原地比较**
5757

5858
此题不同于 [48. 旋转图像](https://leetcode.cn/problems/rotate-image/),并不要求改动原数组,因此,只要比较对应的位置即可。
5959

0 commit comments

Comments
 (0)