原题链接在这里: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. 1 <= N <= 10000
  2. 1 <= connections.length <= 10000
  3. 1 <= connections[i][0], connections[i][1] <= N
  4. 0 <= connections[i][2] <= 10^5
  5. connections[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的更多相关文章

  1. 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...

  2. 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  3. [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 ...

  4. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  5. LeetCode 1000. Minimum Cost to Merge Stones

    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...

  6. LeetCode 1130. Minimum Cost Tree From Leaf Values

    原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...

  7. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

  8. 【LeetCode】1167. Minimum Cost to Connect Sticks 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetcod ...

  9. 【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 ...

随机推荐

  1. Word 图片表格自动编号、交叉引用、批量更改图片标题格式、生成图录和表录

    1. 前言 论文往往里往往需要插入很多图片,下放需要标上 图a-b,其中 a 是章节号码,b是该章节中第几张图.比如第一章第二副图就是 图1-2.但是有个问题,每次我们插入了一张图或删掉了一张,前后的 ...

  2. LeetCode 1223. 掷骰子模拟 Dice Roll Simulation - Java - DP

    题目链接:1223. 掷骰子模拟 有一个骰子模拟器会每次投掷的时候生成一个 1 到 6 的随机数. 不过我们在使用它时有个约束,就是使得投掷骰子时,连续 掷出数字 i 的次数不能超过 rollMax[ ...

  3. Go基础编程实践(五)—— 错误和日志

    自定义错误类型 Go中可以使用errors.New()创建错误信息,也可以通过创建自定义错误类型来满足需求.error是一个接口类型,所有实现该接口的类型都可以当作一个错误类型. // error类型 ...

  4. git学习笔记 ---添加远程库

    现在的情景是,你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一举 ...

  5. 解决本地Bootstrap字体图标不可见的问题

    原文:https://www.jianshu.com/p/70ac459d33e7 作为Bootstrap的初学者,我最近遇到了一个问题:在使用Bootstrap字体图标时,图标不可见.使用代码如下: ...

  6. C# vb .net实现透视阴影特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的透视阴影特效效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 ...

  7. Java之路---Day10(抽象)

    2019-10-24-23:21:17 目录 1.抽象的方法 2.抽象类 3.抽象类和抽象方法的使用 4.抽象类的注意事项 5.案例代码 1.抽象的方法 What:如果父类当中的方法不确定如何进行{} ...

  8. IOWebSocketChannel.connect handle errors

    https://github.com/dart-lang/web_socket_channel/issues/38 yes, my workaround is to create a WebSocke ...

  9. 【开发工具】- Java开发必知工具

    压力测试工具_JMeter 作用 1.能够对HTTP和FTP服务器进行压力和性能测试, 也可以对任何数据库进行同样的测试(通过JDBC). 2.完全的可移植性和100% 纯java. 3.完全 Swi ...

  10. HTTP协议学习总结

    一个web应用程序,往往是通过http协议进行前后端通信的.而作为一个web工程师,掌握HTTP协议因此也是Web开发必备的一项技能了,尤其是在工作了一定年限之后,更是深感该知识点的重要性.因此,将以 ...