-
Notifications
You must be signed in to change notification settings - Fork 16
/
MinCostToConnectAllPoints.java
82 lines (70 loc) · 2.38 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
// P = |points|, E = P^2
// T: O(E logE + E al(P)) al = inverse Ackerman function
// S: O(P + E) = S(E)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class MinCostToConnectAllPoints {
private static class DisjointSet {
private final int[] roots, rank;
DisjointSet(int size) {
roots = new int[size];
rank = new int[size];
for (int i = 0 ; i < size ; i++) {
roots[i] = i;
rank[i] = i;
}
}
public int find(int num) {
if (num == roots[num]) {
return num;
}
return roots[num] = find(roots[num]);
}
public boolean areConnected(int x, int y) {
return find(x) == find(y);
}
public void union(int x, int y) {
final int rootX = find(x), rootY = find(y);
if (rootX == rootY) {
return;
}
if (rank[rootX] > rank[rootY]) {
roots[rootY] = rootX;
} else if (rank[rootX] < rank[rootY]) {
roots[rootX] = rootY;
} else {
roots[rootY] = rootX;
rank[rootX]++;
}
}
}
// Kruskal's algorithm
public int minCostConnectPoints(int[][] points) {
final List<int[]> edges = createEdges(points);
final DisjointSet disjointSet = new DisjointSet(points.length);
edges.sort(Comparator.comparingInt(a -> a[2]));
int minCost = 0;
for (int[] edge : edges) {
final int from = edge[0], to = edge[1], weight = edge[2];
if (!disjointSet.areConnected(from, to)) {
disjointSet.union(from, to);
minCost += weight;
}
}
return minCost;
}
private static List<int[]> createEdges(int[][] points) {
final List<int[]> edges = new ArrayList<>();
for (int i = 0 ; i < points.length ; i++) {
for (int j = i + 1 ; j < points.length ; j++) {
edges.add(new int[] { i, j, manhattanDistance(points[i], points[j]) });
}
}
return edges;
}
private static int manhattanDistance(int[] p1, int[] p2) {
return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);
}
}