LeetCode 1135. Connecting Cities With Minimum Cost
原题链接在这里:https://leetcode.com/problems/connecting-cities-with-minimum-cost/
题目:
There are N cities numbered from 1 to N.
You are given connections, where each connections[i] = [city1, city2, cost] represents the cost to connect city1 and city2together. (A connection is bidirectional: connecting city1 and city2 is the same as connecting city2 and city1.)
Return the minimum cost so that for every pair of cities, there exists a path of connections (possibly of length 1) that connects those two cities together. The cost is the sum of the connection costs used. If the task is impossible, return -1.
Example 1:

Input: N = 3, connections = [[1,2,5],[1,3,6],[2,3,1]]
Output: 6
Explanation:
Choosing any 2 edges will connect all cities so we choose the minimum 2.
Example 2:

Input: N = 4, connections = [[1,2,3],[3,4,4]]
Output: -1
Explanation:
There is no way to connect all cities even if all edges are used.
Note:
1 <= N <= 100001 <= connections.length <= 100001 <= connections[i][0], connections[i][1] <= N0 <= connections[i][2] <= 10^5connections[i][0] != connections[i][1]
题解:
Try to connect cities with minimum cost, then find small cost edge first, if two cities connected by the edge do no have same ancestor, then union them.
When number of unions equal to 1, all cities are connected.
Time Complexity: O(mlogm + mlogN). sort takes O(mlogm). find takes O(logN). With path compression and unino by weight, amatorize O(1).
Space: O(N).
AC Java:
class Solution {
public int minimumCost(int N, int[][] connections) {
Arrays.sort(connections, (a, b) -> a[2]-b[2]);
int res = 0;
UF uf = new UF(N);
for(int [] connect : connections){
if(uf.find(connect[0]) != uf.find(connect[1])){
uf.union(connect[0], connect[1]);
res += connect[2];
}
if(uf.count == 1){
return res;
}
}
return -1;
}
}
class UF{
int [] parent;
int [] size;
int count;
public UF(int n){
parent = new int[n+1];
size = new int[n+1];
for(int i = 0; i<=n; i++){
parent[i] = i;
size[i] = 1;
}
this.count = n;
}
public int find(int i){
if(i != parent[i]){
parent[i] = find(parent[i]);
}
return parent[i];
}
public void union(int p, int q){
int i = find(p);
int j = find(q);
if(size[i] > size[j]){
parent[j] = i;
size[i] += size[j];
}else{
parent[i] = j;
size[j] += size[i];
}
this.count--;
}
}
LeetCode 1135. Connecting Cities With Minimum Cost的更多相关文章
- 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...
- 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- LeetCode 1000. Minimum Cost to Merge Stones
原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...
- LeetCode 1130. Minimum Cost Tree From Leaf Values
原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- 【LeetCode】1167. Minimum Cost to Connect Sticks 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetcod ...
- 【leetcode】1217. Minimum Cost to Move Chips to The Same Position
We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to ...
随机推荐
- Python之颜色的表示
字背景颜色范围:40----49 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色 字颜色:30-----------39 30:黑 31:红 32:绿 33 ...
- centos实现三个节点高可用
centos实现三个节点高可用 使用的资源为keepalived和nginx 高可用主机IP地址 192.168.136.131 192.168.136.133 192.168.136.134 ngi ...
- ZYNQ笔记(0):C语言基础知识复习
ZYNQ的SDK是用C语言进行开发的,C语言可以说是当今理工类大学生的必备技能.我本科学C语言时就是对付考试而已,导致现在学ZYNQ是一脸懵逼.现在特开一帖,整理一下C语言的基础知识. 一.定义 1. ...
- python之数据解构和算法进阶
1.解压赋值多个变量 采用解构的方法.可迭代对象才可以,变量数量与元素个数要一一对应,或者采用*万能接收. 2.解压可迭代对象赋值多个变量 如果一个可迭代对象的元素个数超过变量个数时,会抛出一个 Va ...
- python_并行与并发、多线程
问题一: 计算机是如何执行程序指令的? 问题二: 计算机如何实现并发的? 轮询调度实现并发执行 程序1-8轮询完成,才再CPU上运行 问题三: 真正的并行需要依赖什么? 并行需要的核心条件 多进程实现 ...
- cas sso docker部署service
cas协议: 1. 拉取镜像 docker pull apereo/cas:${tag} 2. 启动容器 docker run --name cas -p : -p : apereo/cas:v5.3 ...
- 定时任务-Windows任务
定时任务-Windows任务 什么是windows任务 windows系统自带一个任务管理组件.可以执行自己写的程序,发送电子邮件(需要邮件服务器),显示消息(就是桌面弹出一个窗口).用的最多的就 ...
- Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)
Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...
- MySql表、字段、库的字符集修改及查看方法
这篇文章主要介绍了MySql表.字段.库的字符集修改及查看方法,本文分别给们它的修改及查看语句,需要的朋友可以参考下 修改数据库字符集: 代码如下: ALTER DATABASE db_name DE ...
- 彻底弄懂ES6中的Map和Set
Map Map对象保存键值对.任何值(对象或者原始值) 都可以作为一个键或一个值.构造函数Map可以接受一个数组作为参数. Map和Object的区别 一个Object 的键只能是字符串或者 Symb ...