-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1135.connecting-cities-with-minimum-cost.java
More file actions
61 lines (58 loc) · 1.64 KB
/
Copy path1135.connecting-cities-with-minimum-cost.java
File metadata and controls
61 lines (58 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Arrays;
/*
* @lc app=leetcode id=1135 lang=java
*
* [1135] Connecting Cities With Minimum Cost
*/
// @lc code=start
class Solution {
public int minimumCost(int N, int[][] connections) {
// 先把所有连接按照cost从小到大排列
Arrays.sort(connections, (a, b) -> {
return a[2] - b[2];
});
int costSum = 0;
int[] root = new int[N + 1];
Arrays.fill(root, -1);
for (int [] connection : connections) {
// 如果当前两个edge可以连接,则把cost加到Sum里
// 并且确认是否只剩下一个edge了,如果是,说明全部都连在一起了
if (union(connection[0], connection[1], root)) {
costSum += connection[2];
N--;
if (N == 1) {
return costSum;
}
}
}
return -1;
}
private boolean union(int a, int b, int[] root) {
int rootA = find(a, root), rootB = find(b, root);
if (rootA == rootB) {
return false;
}
int sizeA = root[rootA], sizeB = root[rootB];
if (sizeA < sizeB) {
root[rootA] += sizeB;
root[rootB] = rootA;
}
else {
root[rootB] += sizeA;
root[rootA] = rootB;
}
return true;
}
private int find(int x, int[] root) {
int rootX = root[x];
if (rootX < 0) {
return x;
}
if (root[rootX] > 0) {
rootX = find(rootX, root);
root[x] = rootX;
}
return rootX;
}
}
// @lc code=end