-
Notifications
You must be signed in to change notification settings - Fork 277
/
MinCostToConnectAllPoints.java
46 lines (33 loc) · 1.29 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
// @saorav21994
// TC : O(n^2) -> we need to find cost of all possible edges
// SC : O(n)
// Prim's algorithm for finding minimum spanning tree without using heap to reduce time complexity.
class Solution {
public int minCostConnectPoints(int[][] points) {
int n = points.length;
int minCost = 0;
int edges = 0;
int [] cost = new int[n];
boolean [] visited = new boolean[n];
Arrays.fill(cost, Integer.MAX_VALUE);
cost[0] = 0; // starting node
for ( ; edges < n; edges++) {
int curCost = Integer.MAX_VALUE;
int curPoint = -1;
for (int i = 0; i < n; i++) {
if (!visited[i] && cost[i] < curCost) {
curCost = cost[i];
curPoint = i;
}
}
minCost += curCost;
visited[curPoint] = true;
for (int i = 0; i < n; i++) {
curCost = Math.abs(points[i][0] - points[curPoint][0]) + Math.abs(points[i][1] - points[curPoint][1]);
if (!visited[i] && cost[i] > curCost)
cost[i] = curCost;
}
}
return minCost;
}
}