-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1584.java
64 lines (56 loc) · 1.58 KB
/
1584.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
class Edge {
int point1;
int point2;
int len;
public Edge(int point1, int point2, int len) {
this.point1 = point1;
this.point2 = point2;
this.len = len;
}
}
class Solution {
int[] parent;
public int minCostConnectPoints(int[][] points) {
int n = points.length;
if (n < 2) return 0;
Set<Integer> set = new HashSet<>();
Queue<Edge> queue = new PriorityQueue<>((a,b) -> a.len-b.len);
int res = 0;
int count = n-1;
parent = new int[n];
for (int i = 1; i < n; i++) {
parent[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
int dist = distance(points[i][0], points[i][1], points[j][0], points[j][1]);
queue.offer(new Edge(i, j, dist));
}
}
while (count > 0) {
Edge curr = queue.poll();
if (find(curr.point1) == find(curr.point2)) continue;
res += curr.len;
set.add(curr.point1);
set.add(curr.point2);
union(curr.point1, curr.point2);
count--;
}
return res;
}
private int distance(int x1, int y1, int x2, int y2) {
return Math.abs(x1-x2) + Math.abs(y1-y2);
}
private int find(int x) {
if (parent[x] == x) return x;
parent[x] = find(parent[x]);
return parent[x];
}
private void union(int x, int y) {
int px = find(x);
int py = find(y);
if (px != py) {
parent[px] = py;
}
}
}