/
MinCostToConnectAllPoints.java
82 lines (67 loc) · 2.09 KB
/
MinCostToConnectAllPoints.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// https://leetcode.com/problems/min-cost-to-connect-all-points
class Solution {
public int minCostConnectPoints(int[][] points) {
List<Edge> edges = new ArrayList<>();
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
edges.add(new Edge(points[i], points[j], i, j));
}
}
Collections.sort(edges);
int min = 0;
UnionFind uf = new UnionFind(points.length);
for (Edge e: edges) {
int rootFrom = uf.find(e.from);
int rootTo = uf.find(e.to);
if (rootFrom != -1 && rootTo != -1 && rootFrom != rootTo) {
min += e.weight;
uf.union(e.from, e.to);
}
}
return min;
}
class Edge implements Comparable<Edge> {
int from, to, weight;
Edge(int[] src, int[] dst, int from, int to) {
this.from = from;
this.to = to;
this.weight = dist(src, dst);
}
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}
int dist(int[] src, int[] dst) {
return Math.abs(src[0] - dst[0]) + Math.abs(src[1] - dst[1]);
}
class UnionFind {
int[] root;
int[] rank;
UnionFind(int size) {
root = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
root[i] = i;
rank[i] = i;
}
}
int find(int x) {
if (x == root[x]) {
return x;
}
return root[x] = find(root[x]);
}
void union(int a, int b) {
int rootA = find(a);
int rootB = find(b);
if (rootA < rootB) {
root[rootA] = rootB;
} else if (rootB < rootA) {
root[rootB] = rootA;
} else {
root[rootB] = rootA;
rank[rootA] += 1;
}
}
}
}