Prim with arrays
Time O(n²) Space O(n)best[v] = cheapest connection to the tree. Repeat n times: pick the cheapest outside point, add its cost, update best for the rest.
import java.util.Arrays;
class Solution {
public int minCostConnectPoints(int[][] points) {
int n = points.length, total = 0;
int[] best = new int[n];
Arrays.fill(best, Integer.MAX_VALUE);
best[0] = 0;
boolean[] inTree = new boolean[n];
for (int step = 0; step < n; step++) {
int u = -1;
for (int v = 0; v < n; v++) if (!inTree[v] && (u == -1 || best[v] < best[u])) u = v;
inTree[u] = true;
total += best[u];
for (int v = 0; v < n; v++) {
if (inTree[v]) continue;
int d = Math.abs(points[u][0] - points[v][0]) + Math.abs(points[u][1] - points[v][1]);
if (d < best[v]) best[v] = d;
}
}
return total;
}
}Verdict: Optimal for complete graphs.