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. 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: 

复制代码
 1 class Solution {
 2     public int minimumCost(int N, int[][] connections) {
 3         Arrays.sort(connections, (a, b) -> a[2]-b[2]);
 4         
 5         int res = 0;
 6         UF uf = new UF(N);
 7         for(int [] connect : connections){
 8             if(uf.find(connect[0]) != uf.find(connect[1])){
 9                 uf.union(connect[0], connect[1]);
10                 res += connect[2];
11             }
12             
13             if(uf.count == 1){
14                 return res;
15             }
16         }
17         
18         return -1;
19     }
20 }
21 
22 class UF{
23     int [] parent;
24     int [] size;
25     int count;
26     
27     public UF(int n){
28         parent = new int[n+1];
29         size = new int[n+1];
30         for(int i = 0; i<=n; i++){
31             parent[i] = i;
32             size[i] = 1;
33         }
34         
35         this.count = n;
36     }
37     
38     public int find(int i){
39         if(i != parent[i]){
40             parent[i] = find(parent[i]);
41         }
42         
43         return parent[i];
44     }
45     
46     public void union(int p, int q){
47         int i = find(p);
48         int j = find(q);
49         if(size[i] > size[j]){
50             parent[j] = i;
51             size[i] += size[j];
52         }else{
53             parent[i] = j;
54             size[j] += size[i];
55         }
56         
57         this.count--;
58     }
59 }
复制代码

 

posted @   Dylan_Java_NYC  阅读(3625)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2018-08-01 LeetCode 759. Employee Free Time
点击右上角即可分享
微信分享提示