当前位置:网站首页>LeetCode 1584. Minimum cost of connecting all points
LeetCode 1584. Minimum cost of connecting all points
2022-07-06 16:43:00 【Daylight629】
1584. The minimum cost of connecting all points
To give you one points
Array , Express 2D Some points on the plane , among points[i] = [xi, yi]
.
Connection point [xi, yi]
Sum point [xj, yj]
The cost is between them Manhattan distance :|xi - xj| + |yi - yj|
, among |val|
Express val
The absolute value of .
Please return the minimum total cost of connecting all points . Just between any two points Yes and no A simple path , All the points are connected .
Example 1:
Input :points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
Output :20
explain :
We can connect all the points as shown in the figure above to get the minimum total cost , The total cost is 20 .
Notice that there is only one path between any two points to reach each other .
Example 2:
Input :points = [[3,12],[-2,5],[-4,1]]
Output :18
Example 3:
Input :points = [[0,0],[1,1],[1,0],[-1,1]]
Output :4
Example 4:
Input :points = [[-1000000,-1000000],[1000000,1000000]]
Output :4000000
Example 5:
Input :points = [[0,0]]
Output :0
Tips :
1 <= points.length <= 1000
-106 <= xi, yi <= 106
- All points
(xi, yi)
Two are different .
Two 、 Method 1
Kruskal Algorithm
class Solution {
public int minCostConnectPoints(int[][] points) {
int n = points.length;
DisjointSetUnion dsu = new DisjointSetUnion(n);
List<Edge> edges = new ArrayList<Edge>();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
edges.add(new Edge(dist(points, i, j), i, j));
}
}
Collections.sort(edges, new Comparator<Edge>() {
public int compare(Edge edge1, Edge edge2) {
return edge1.len - edge2.len;
}
});
int ret = 0, num = 1;
for (Edge edge : edges) {
int len = edge.len, x = edge.x, y = edge.y;
if (dsu.unionSet(x, y)) {
ret += len;
num++;
if (num == n) {
break;
}
}
}
return ret;
}
public int dist(int[][] points, int x, int y) {
return Math.abs(points[x][0] - points[y][0]) + Math.abs(points[x][1] - points[y][1]);
}
}
class DisjointSetUnion {
int[] f;
int[] rank;
int n;
public DisjointSetUnion(int n) {
this.n = n;
this.rank = new int[n];
Arrays.fill(this.rank, 1);
this.f = new int[n];
for (int i = 0; i < n; i++) {
this.f[i] = i;
}
}
public int find(int x) {
return f[x] == x ? x : (f[x] = find(f[x]));
}
public boolean unionSet(int x, int y) {
int fx = find(x), fy = find(y);
if (fx == fy) {
return false;
}
if (rank[fx] < rank[fy]) {
int temp = fx;
fx = fy;
fy = temp;
}
rank[fx] += rank[fy];
f[fy] = fx;
return true;
}
}
class Edge {
int len, x, y;
public Edge(int len, int x, int y) {
this.len = len;
this.x = x;
this.y = y;
}
}
Complexity analysis
Time complexity :O(n2 log(n)), among n It's the number of nodes . commonly Kruskal yes O(mlogm) The algorithm of , But in this question m=n^2
, So the total time complexity is O(n2log(n)).Spatial complexity :O(n^2), among n It's the number of nodes . Use of parallel search set O(n) Space , The edge set array needs to use O(n^2) Space .
边栏推荐
- (lightoj - 1323) billiard balls (thinking)
- Click QT button to switch qlineedit focus (including code)
- Codeforces Round #802(Div. 2)A~D
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Problem - 1646C. Factorials and Powers of Two - Codeforces
- Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
- Detailed explanation of FLV format
- Soft music -js find the number of times that character appears in the string - Feng Hao's blog
- antd upload beforeUpload中禁止触发onchange
- Submit several problem records of spark application (sparklauncher with cluster deploy mode)
猜你喜欢
300th weekly match - leetcode
ffmpeg命令行使用
OneForAll安装使用
第五章 Yarn资源调度器
图像处理一百题(1-10)
Summary of game theory
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
Codeforces Round #801 (Div. 2)A~C
拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
随机推荐
第一章 MapReduce概述
Codeforces Round #800 (Div. 2)AC
Market trend report, technical innovation and market forecast of China's desktop capacitance meter
Codeforces Round #803 (Div. 2)A~C
Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
图像处理一百题(1-10)
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
LeetCode 1560. The sector with the most passes on the circular track
解决Intel12代酷睿CPU单线程调度问题(二)
Market trend report, technological innovation and market forecast of desktop electric tools in China
Problem - 1646C. Factorials and Powers of Two - Codeforces
Li Kou: the 81st biweekly match
js封装数组反转的方法--冯浩的博客
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
Chapter 2 shell operation of hfds
Chapter 6 datanode
Cmake error: could not create named generator visual studio 16 2019 solution
Raspberry pie 4B installation opencv3.4.0
(lightoj - 1349) Aladdin and the optimal invitation (greed)
新手必会的静态站点生成器——Gridsome