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 ...
随机推荐
- mysql 查询导出 excel 中文乱码 解决 --default-character-set=gbk
mysql --default-character-set=gbk -uroot -p -D open_fusion -e " select * from table1 " ...
- quartz与c3p0冲突
在SSM中使用连接池c3p0正常,引入quartz后发现后台报错 java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewP ...
- linux 安装Python3.6
1.安装依赖 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel ...
- 在docker容器上如何实现代码的版本管理
之前在一台centos7的虚拟机上部署了docker并运行了三个容器给开发写代码用,写代码肯定会涉及到版本控制管理. 开始建议是开发在容器中写代码,然后通过docker commit的方式将其保存为i ...
- python基础知识和练习代码
1.编译:将高级语言源代码转换成目标代码(机器语言),程序便可运行. 好处:目标代码执行速度更快:目标代码在相同操作系统上使用灵活. 2.解释:将高级语言源代码逐条转换成目标代码同时逐条执行,每次 ...
- Vue.js的路由之——vue-router快速入门
直接先上效果图 这个单页面应用有两个路径:/home和/about,与这两个路径对应的是两个组件Home和About. 整个实现过程 JavaScript 创建组件:创建单页面应用需要渲染的组件 创建 ...
- day 01 预科
目录 作业 二,Markdown基本语法 一级标题 二级标题 三级标题 作业 二,Markdown基本语法 标题 一级标题 二级标题 三级标题 四级标题 加粗 哦,更粗了 斜体 咦,我歪了 高亮 == ...
- Android笔记(七十六) 点菜DEMO
一个朋友让看一下他的代码,一个点菜的功能,他和我一样,初学者,代码比我的都混乱,也是醉了,干脆想着自己写个demo给他看,原本想着听简单,半个小时应该就可以搞定,真正写的时候,画了3h+,汗颜... ...
- OpenStack云计算简介
1. 云计算的发展 云计算是IT技术不断发展的产物. 要理解云计算,需要对IT系统架构的发展过程有所认识. IT系统架构的发展到目前为止大致可以分为3个阶段: 1> 物理机架构 这一阶段,应用部 ...
- SpringCloud2.0 Hystrix Dashboard 断路器指标看板 基础教程(八)
1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...