|
1 | 1 | class Solution {
|
2 |
| - public int minCostConnectPoints(int[][] points) { |
3 |
| - int n = points.length; |
4 |
| - PriorityQueue<Edge> priorityQueue = new PriorityQueue<>((x, y) -> x.cost - y.cost); |
5 |
| - UnionFind unionFind = new UnionFind(n); |
6 |
| - for (int i = 0; i < n; i++) { |
7 |
| - int[] coordinate = points[i]; |
8 |
| - for (int j = i + 1; j < n; j++) { |
9 |
| - int[] secondCoordinate = points[j]; |
10 |
| - int cost = Math.abs(coordinate[0] - secondCoordinate[0]) + Math.abs(coordinate[1] - secondCoordinate[1]); |
11 |
| - Edge edge = new Edge(i, j, cost); |
12 |
| - priorityQueue.add(edge); |
13 |
| - } |
14 |
| - } |
15 |
| - int minimumCost = 0; |
16 |
| - int totalEdges = n - 1; |
17 |
| - while (!priorityQueue.isEmpty() && totalEdges > 0) { |
18 |
| - Edge edge = priorityQueue.poll(); |
19 |
| - if (!unionFind.connected(edge.pointOne, edge.pointTwo)) { |
20 |
| - unionFind.union(edge.pointOne, edge.pointTwo); |
21 |
| - minimumCost += edge.cost; |
22 |
| - totalEdges--; |
23 |
| - } |
24 |
| - } |
25 |
| - return minimumCost; |
26 |
| - } |
27 |
| - |
28 |
| - private static class Edge { |
29 |
| - int pointOne; |
30 |
| - int pointTwo; |
31 |
| - int cost; |
32 |
| - |
33 |
| - public Edge(int pointOne, int pointTwo, int cost) { |
34 |
| - this.pointOne = pointOne; |
35 |
| - this.pointTwo = pointTwo; |
36 |
| - this.cost = cost; |
| 2 | + public int minCostConnectPoints(int[][] points) { |
| 3 | + int n = points.length; |
| 4 | + List<int[]> edges = new ArrayList<>(); |
| 5 | + for (int i = 0; i < n; i++) { |
| 6 | + for (int j = i + 1; j < n; j++) { |
| 7 | + int weight = Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]); |
| 8 | + int[] curr = {weight, i, j}; |
| 9 | + edges.add(curr); |
| 10 | + } |
| 11 | + } |
| 12 | + Collections.sort(edges, Comparator.comparingInt(a -> a[0])); |
| 13 | + UnionFind unionFind = new UnionFind(n); |
| 14 | + int minCost = 0; |
| 15 | + int edgeUsedCount = 0; |
| 16 | + for (int i = 0; i < edges.size() && edgeUsedCount < n - 1; i++) { |
| 17 | + int nodeOne = edges.get(i)[1]; |
| 18 | + int nodeTwo = edges.get(i)[2]; |
| 19 | + int weight = edges.get(i)[0]; |
| 20 | + if (unionFind.union(nodeOne, nodeTwo)) { |
| 21 | + minCost += weight; |
| 22 | + edgeUsedCount++; |
| 23 | + } |
| 24 | + } |
| 25 | + return minCost; |
37 | 26 | }
|
38 |
| - } |
39 |
| - |
40 |
| - private static class UnionFind { |
41 | 27 |
|
42 |
| - private final int[] root; |
43 |
| - private final int[] rank; |
| 28 | + private static class UnionFind { |
| 29 | + private final int[] group; |
| 30 | + private final int[] rank; |
44 | 31 |
|
45 |
| - public UnionFind(int size) { |
46 |
| - this.root = new int[size]; |
47 |
| - this.rank = new int[size]; |
48 |
| - for (int i = 0; i < size; i++) { |
49 |
| - this.root[i] = i; |
50 |
| - this.rank[i] = 1; |
51 |
| - } |
52 |
| - } |
53 |
| - |
54 |
| - public void union(int nodeOne, int nodeTwo) { |
55 |
| - int rootOne = find(nodeOne); |
56 |
| - int rootTwo = find(nodeTwo); |
57 |
| - if (rootOne != rootTwo) { |
58 |
| - if (this.rank[rootOne] > this.rank[rootTwo]) { |
59 |
| - this.root[rootTwo] = rootOne; |
60 |
| - } else if (this.rank[rootOne] < this.rank[rootTwo]) { |
61 |
| - this.root[rootOne] = rootTwo; |
62 |
| - } else { |
63 |
| - this.root[rootTwo] = rootOne; |
64 |
| - this.rank[rootOne]++; |
| 32 | + public UnionFind(int n) { |
| 33 | + this.group = new int[n]; |
| 34 | + this.rank = new int[n]; |
| 35 | + for (int i = 0; i < n; i++) { |
| 36 | + this.group[i] = i; |
| 37 | + } |
65 | 38 | }
|
66 |
| - } |
67 |
| - } |
68 | 39 |
|
69 |
| - public int find(int node) { |
70 |
| - if (node == root[node]) { |
71 |
| - return node; |
72 |
| - } |
73 |
| - return root[node] = find(root[node]); |
74 |
| - } |
| 40 | + public int find(int node) { |
| 41 | + if (group[node] != node) { |
| 42 | + group[node] = find(group[node]); |
| 43 | + } |
| 44 | + return group[node]; |
| 45 | + } |
75 | 46 |
|
76 |
| - public boolean connected(int nodeOne, int nodeTwo) { |
77 |
| - return find(nodeOne) == find(nodeTwo); |
| 47 | + public boolean union(int nodeOne, int nodeTwo) { |
| 48 | + int groupOne = find(nodeOne); |
| 49 | + int groupTwo = find(nodeTwo); |
| 50 | + if (groupOne == groupTwo) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + if (rank[groupOne] > rank[groupTwo]) { |
| 54 | + group[groupTwo] = groupOne; |
| 55 | + } else if (rank[groupOne] < rank[groupTwo]) { |
| 56 | + group[groupOne] = groupTwo; |
| 57 | + } else { |
| 58 | + group[groupOne] = groupTwo; |
| 59 | + rank[groupTwo]++; |
| 60 | + } |
| 61 | + return true; |
| 62 | + } |
78 | 63 | }
|
79 |
| - } |
80 | 64 | }
|
0 commit comments