leetcode-第5周双周赛-1135-最低成本联通所有城市


方法一:
class Solution:
def minimumCost(self, N: int, conections: List[List[int]]) -> int:
def find(i):
if father[i]==i:
return i
else:
father[i]=find(father[i])
return father[i] conn=sorted(conections,key=lambda item:item[2])
visited=[]
res=0
father=[i for i in range(N+1)]
for item in conn:
i,j=item[0],item[1]
cost=item[2]
fi,fj=find(i),find(j)
if fi==fj:
continue
res+=cost
father[fi]=fj
visited.append((i,j))
return res if len(visited)==N-1 else -1
leetcode-第5周双周赛-1135-最低成本联通所有城市的更多相关文章
- LeetCode第8场双周赛(Java)
这次我只做对一题. 原因是题目返回值类型有误,写的是 String[] ,实际上应该返回 List<String> . 好吧,只能自认倒霉.就当涨涨经验. 5068. 前后拼接 解题思路 ...
- LeetCode第29场双周赛题解
第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: doub ...
- LeetCode 第 15 场双周赛
1287.有序数组中出现次数超过25%的元素 1288.删除被覆盖区间 1286.字母组合迭代器 1289.下降路径最小和 II 下降和不能只保留原数组中最小的两个,hacked. 1287.有序数组 ...
- LeetCode 第 14 场双周赛
基础的 api 还是不够熟悉啊 5112. 十六进制魔术数字 class Solution { public: char *lltoa(long long num, char *str, int ra ...
- leetcode-第14周双周赛-1274-矩形内船只的数目
题目描述: 自己的提交: # """ # This is Sea's API interface. # You should not implement it, or s ...
- leetcode-第14周双周赛-1273-删除树节点
题目描述: 自己的提交:动态规划 class Solution: def deleteTreeNodes(self, nodes: int, parent: List[int], value: Lis ...
- leetcode-第14周双周赛-1272-删除区间
题目描述: 自己的提交: class Solution: def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[ ...
- leetcode-第14周双周赛-1271-十六进制魔术数字
自己的提交: class Solution: def toHexspeak(self, num: str) -> str: num = hex(int(num)) num = str(num)[ ...
- leetcode-第12周双周赛-5111-分享巧克力
题目描述: 方法: class Solution: def maximizeSweetness(self, A: List[int], K: int) -> int: def possible( ...
- leetcode-12周双周赛-5090-抛掷硬币
题目描述: 二维dp: class Solution: def probabilityOfHeads(self, prob: List[float], target: int) -> float ...
随机推荐
- faster-rcnn代码阅读-rpn-data层
这一节讲述rpn-data层,和这一层有关的结构图如下: rpn-data层的prototxt定义如下: layer { name: 'rpn-data' type: 'Python' bottom: ...
- ionic-CSS:ionic 颜色
ylbtech-ionic-CSS:ionic 颜色 1.返回顶部 1. ionic 颜色 ionic 提供了很多颜色的配置,当然你可以根据自己的需要自定义颜色. 实例 <ul class=&q ...
- 312. 戳气球【困难】【区间DP】
题目链接 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * ...
- Error resolving template,template might not exist or might not be accessible by any of the configured Template Resolvers
template might not exist or might not be accessible by any of the configured Template Resolvers at o ...
- 一点响应式Web设计与实现思路
摘要: 是否还在为你的应用程序适配PC端,移动端,平板而苦苦思索呢,是否在寻找如何一套代码适配多终端方式呢,是否希望快速上手实现你的跨终端应用程序呢,是的话,那就看过来吧,本文阐述响应式UI设计相关理 ...
- Ubuntu 最简单的方式安装chrome
1.指定安装目录如下: cd opt/ 2.下载包: sudo wget https://dl.google.com/linux/direct/google-chrome-stable_current ...
- python2x 安装 psutil
安装psutil模块: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz --no-check-cer ...
- redis安装配置使用
1.redis官方下载地址:https://redis.io/download 也可以github上下载,redis 64位下载地址:https://github.com/ServiceStack/r ...
- SQL Server 2008 install
双击sql server 2008的.exe安装文件,进入[SQL Server 安装中心]. 2 点击界面左侧的[安装],然后点击右侧的[全新SQL Server 独立安装或向现有安装添加功能],进 ...
- 概率+后效性处理——cf930B好题
之前题目看错了.. 先用双倍字符串处理后效性 首先要确定一个结论:如果原串s中相距为d的ch1和ch2只有一对,那么如果第一个翻开ch1,第二个翻开ch2,就能确定k 现在要求的是当我们第一次翻开的是 ...