Repository navigation
Expand file tree
/
Copy pathConnectTheDots.java
More file actions
82 lines (77 loc) · 2.58 KB
/
Copy pathConnectTheDots.java
File metadata and controls
82 lines (77 loc) · 2.58 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class UnionFind {
int[] parent;
int[] size;
public UnionFind(int size) {
this.parent = new int[size];
this.size = new int[size];
for (int i = 0; i < size; i++) {
this.parent[i] = i;
}
Arrays.fill(this.size, 1);
}
public boolean union(int x, int y) {
int repX = find(x);
int repY = find(y);
if (repX != repY) {
if (this.size[repX] > this.size[repY]) {
this.parent[repY] = repX;
this.size[repX] += this.size[repY];
}
else {
this.parent[repX] = repY;
this.size[repY] += this.size[repX];
}
// Return True if both groups were merged.
return true;
}
// Return False if the points belong to the same group.
return false;
}
public int find(int x) {
if (x == this.parent[x]) {
return x;
}
this.parent[x] = find(this.parent[x]);
return this.parent[x];
}
}
public class ConnectTheDots {
public int connectTheDots(int[][] points) {
int n = points.length;
// Create and populate a list of all possible edges.
List<int[]> edges = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Manhattan distance.
int cost = Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]);
edges.add(new int[]{cost, i, j});
}
}
// Sort the edges by their cost in ascending order.
Collections.sort(edges, (a, b) -> Integer.compare(a[0], b[0]));
UnionFind uf = new UnionFind(n);
int totalCost, edgesAdded;
totalCost = edgesAdded = 0;
// Use Kruskal's algorithm to create the MST and identify its minimum cost.
for (int[] edge : edges) {
int cost = edge[0];
int p1 = edge[1];
int p2 = edge[2];
// If the points are not already connected (i.e. their representatives are
// not the same), connect them, and add the cost to the total cost.
if (uf.union(p1, p2)) {
totalCost += cost;
edgesAdded++;
// If n - 1 edges have been added to the MST, the MST is complete.
if (edgesAdded == n - 1) {
return totalCost;
}
}
}
return 0;
}
}