You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].
The cost of connecting two points [xi, yi] and [xj, yj] is the manhattan distance between them: |xi - xj| + |yi - yj|, where |val| denotes the absolute value of val.
Return the minimum cost to make all points connected. All points are connected if there is exactly one simple path between any two points.
Input: points =[[0,0],[2,2],[3,10],[5,2],[7,0]]Output: 20Explanation:
We can connect the points as shown above to get the minimum cost of 20.Notice that there is a unique path between every pair of points.
In this problem, we already know that given points are the nodes. The distance between each of the nodes, i.e. the manhattan distance is the edges. We want min of these distance, which means this is like Prim’s algorithm. Read here - Minimum Spanning Trees MST - Prim’s Algorithm and also we see
classSolution {
public:int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size(), ans =0, cnt =0;
vector<bool> vis(n, false);
vector<int> minDist(n, INT_MAX);
minDist[0] =0;
for (int i =0; i < n; ++i) {
int u =-1;
for (int v =0; v < n; ++v) {
if (!vis[v] && (u ==-1|| minDist[v] < minDist[u])) u = v;
}
vis[u] = true;
ans += minDist[u];
for (int v =0; v < n; ++v) {
int cost = abs(points[u][0] - points[v][0]) + abs(points[u][1] - points[v][1]);
if (!vis[v] && cost < minDist[v]) minDist[v] = cost;
}
}
return ans;
}
};
classSolution {
publicintminCostConnectPoints(int[][] points) {
int n = points.length, ans = 0;
boolean[] vis =newboolean[n];
int[] minDist =newint[n];
Arrays.fill(minDist, Integer.MAX_VALUE);
minDist[0]= 0;
for (int i = 0; i < n; i++) {
int u =-1;
for (int v = 0; v < n; v++) {
if (!vis[v]&& (u ==-1 || minDist[v]< minDist[u])) u = v;
}
vis[u]=true;
ans += minDist[u];
for (int v = 0; v < n; v++) {
int cost = Math.abs(points[u][0]- points[v][0]) + Math.abs(points[u][1]- points[v][1]);
if (!vis[v]&& cost < minDist[v]) minDist[v]= cost;
}
}
return ans;
}
}
classSolution {
funminCostConnectPoints(points: Array<IntArray>): Int {
val n = points.size
val vis = BooleanArray(n)
val minDist = IntArray(n) { Int.MAX_VALUE }
minDist[0] = 0var ans = 0 repeat(n) {
var u = -1for (v in0 until n) {
if (!vis[v] && (u == -1|| minDist[v] < minDist[u])) u = v
}
vis[u] = true ans += minDist[u]
for (v in0 until n) {
val cost = Math.abs(points[u][0] - points[v][0]) + Math.abs(points[u][1] - points[v][1])
if (!vis[v] && cost < minDist[v]) minDist[v] = cost
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defminCostConnectPoints(self, points: list[list[int]]) -> int:
n = len(points)
vis = [False] * n
min_dist = [float('inf')] * n
min_dist[0] =0 ans =0for _ in range(n):
u =-1for v in range(n):
ifnot vis[v] and (u ==-1or min_dist[v] < min_dist[u]):
u = v
vis[u] =True ans += min_dist[u]
for v in range(n):
cost = abs(points[u][0] - points[v][0]) + abs(points[u][1] - points[v][1])
ifnot vis[v] and cost < min_dist[v]:
min_dist[v] = cost
return ans
We can also solve the minimum cost to connect all points by building all possible edges (with their Manhattan distances) and sorting them by cost. Then, using Kruskal’s algorithm, we add the smallest edges one by one, connecting components using Union-Find, until all points are connected. This approach is efficient when the number of points is not too large.
⏰ Time complexity: O(n^2 \, \log n) — Building all edges takes O(n^2), sorting takes O(n^2 \log n), and union-find operations are nearly constant time.
🧺 Space complexity: O(n^2) — For storing all possible edges.