-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsol.java
56 lines (49 loc) · 1.71 KB
/
sol.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
import java.util.LinkedList;
import java.util.Queue;
public class sol {
public static void main(String[] args) {
// call the fn here
}
class Solution {
public int minCostConnectPoints(int[][] points) {
int n = points.length;
int[][] matrix = new int[n][n];
boolean[] visited = new boolean[n];
Queue<Integer> queue = new LinkedList<>();
int distance = 0;
// get all distance
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]);
}
;
}
// first
queue.add(0);
visited[0] = true;
while (queue.size() < n) {
int sizes = queue.size();
int len = Integer.MAX_VALUE;
int choosen = 0;
int nextPoint = 0;
for (int ss = 0; ss < sizes; ss++) {
int connect = queue.poll();
for (int next = 0; next < n; next++) {
if (!visited[next]) {
if (matrix[connect][next] < len) {
choosen = connect;
nextPoint = next;
len = matrix[connect][next];
}
}
}
queue.add(connect);
}
visited[nextPoint] = true;
queue.add(nextPoint);
distance += len;
}
return distance;
}
}
}