743. Network Delay Time
题目来源:
https://leetcode.com/problems/network-delay-time/
自我感觉难度/真实难度:
题意:
分析:
自己的代码:
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
K-=1
nodes=collections.defaultdict(list)
for u,v,w in times:
nodes[u-1].append((v-1,w))
dist=[float('inf')]*N
dist[K]=0
done=set()
for _ in range(N):
smallest=min((d,i) for (i,d) in enumerate(dist) if i not in done)[1]
for v,w in nodes[smallest]:
if v not in done and dist[smallest]+w<dist[v]:
dist[v]=dist[smallest]+w
done.add(smallest)
return -1 if float('inf') in dist else max(dist)
代码效率/结果:
Runtime: 152 ms, faster than 83.77% of Python3 online submissions forNetwork Delay Time.
优秀代码:
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
h = [(0, K)]
res = 0
reached = set()
d = {}
for u, v, w in times:
if u not in d:
d[u] = []
d[u].append([v, w])
while h:
t, n = heapq.heappop(h)
if n not in reached:
res = t
reached.add(n)
if n in d:
for v, w in d[n]:
if v not in reached:
heapq.heappush(h, [t + w, v])
if len(reached) < N:
return -1
return res
优秀的结题报告,提供了三种超级棒的解答方法:https://blog.csdn.net/fuxuemingzhu/article/details/82862769




第一次写图的代码,其实草稿纸推一推,发现也不是特别的难,不要畏惧
代码效率/结果:
自己优化后的代码:
反思改进策略:
1.对heapq的模块不熟悉,后面几天着重练习一下堆栈这一块的练习
2.对dijstra算法不熟悉,希望把上面的模块背出来
3.
float('inf')表示正无穷
float('-inf')表示负无穷
4.
dist=[float('inf')]*N
初始化List简单的办法
743. Network Delay Time的更多相关文章
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- [LeetCode] 743. Network Delay Time 网络延迟时间
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...
- LeetCode 743. Network Delay Time
原题链接在这里:https://leetcode.com/problems/network-delay-time/ 题目: There are N network nodes, labelled 1 ...
- [LeetCode] Network Delay Time 网络延迟时间
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...
- [Swift]LeetCode743. 网络延迟时间 | Network Delay Time
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...
- [LeetCode] Network Delay Time 网络延迟时间——最短路算法 Bellman-Ford(DP) 和 dijkstra(本质上就是BFS的迭代变种)
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...
- 【leetcode】Network Delay Time
题目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edge ...
- leetcode743 Network Delay Time
""" here are N network nodes, labelled 1 to N. Given times, a list of travel times as ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- 在vue中子组件修改props引发的对js深拷贝和浅拷贝的思考
不管是react还是vue,父级组件与子组件的通信都是通过props来实现的,在vue中父组件的props遵循的是单向数据流,用官方的话说就是,父级的props的更新会向下流动到子组件中,反之则不行. ...
- Oracle数据库拼接字符串
Oracle数据库中字符串拼接的实现 select count() from THT_HTFKJL where ht=1: 假如结果为:31.例如上面例子想要给结果拼接字符串有二种实现方法,第一种使用 ...
- 解决 login.live.com onedrive.live.com 等微软国外网站打不开问题
下面就分享一下通过更改HOSTS文件的方式打开onedrive网页版的方法. C:\Windows\System32\drivers\etc目录下的hosts文件把它复制到D盘,再复制一份放到桌面上. ...
- spring shiro 集成
1.向spring项目中添加shiro相关的依赖 <dependency> <groupId>commons-logging</groupId> <artif ...
- 【转】教你弄清 OSX 的睡眠模式,以及合法的禁止产生 sleepimage
原文链接 因为之前用的是网上流传的土法来禁止生成 sleepimage,尝到了苦头,而且2次! 大家知道 OSX 有几种睡眠模式,其中 hibernatemode 可以是 0 (传统睡眠方式,不生成 ...
- Selenium clear()方法无法清掉数据
问题描述 clear()方法执行过后, 数据还是在. 根本原因 存在镜像节点. 操作clear()清掉数据后, 镜像节点的数据还在, 就会再补充回去. 解决办法 添加下面代码就可以连同镜像的数据一起去 ...
- 6.JXL操作Excel
一.简介 jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelA ...
- 【转】Kettle发送邮件步骤遇到附件名是中文名变成乱码的问题解决办法
原文:http://www.ukettle.org/thread-607-1-1.html 本帖最后由 大白菜 于 2016-3-7 10:18 编辑 导语:看到群里很多朋友问Kettle发送邮件附件 ...
- python基础_类型_tuple
#tuple 元祖,这个没什么特别的,和list差不多,不能删除,不能增加元素,其他功能差不多 #元祖用圆括号扩起来,逗号分隔 a = ('a','b','c') #这玩意一般会用来排除重复,还是很好 ...
- linux下jira搭建&破解(转自:https://www.cnblogs.com/zpw-1/p/9553358.html)
写在前面 网络类似文章不少,但是同样的路,别人走可能一马平川,自己走可能磕磕绊绊.记录一下自己搭建过程的一路踩坑历程[目前还记得的]. 一.环境准备 1,jira7.3的运行是依赖java环境的,也就 ...