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总的的边数减去重叠的 ...
随机推荐
- js-权威指南学习笔记16
1.元素的style属性可以用来设置样式,但是不适合用来查询样式(只能查询到内联样式). 2.CSS里的层叠指示了应用于文档中任何给定元素的样式规则是各个来源的层叠效果:Web浏览器的默认样式表.文档 ...
- C# 获取客户端信息 /asp.net/WebService/WebForm
Request.Browser.MajorVersion.ToString();//获取客户端浏览器的(主)版本号Request.Browser.Version.ToString(); //获取客 ...
- 产品相关 细说软件产品和业务 & 业务过程(流程) & 业务逻辑
细说软件产品和业务& 业务过程(流程) & 业务逻辑 by:授客 QQ:1033553122 作为一名测试人猿,需要懂产品,不懂产品的测试猿不是好测试猿猴.而业务逻辑是软件产品 ...
- 网络 IP地址、网段、子网掩码
IP地址范围,最小:00000000,00000000,00000000,00000000:最大:11111111,11111111,11111111,11111111 即:最小:0.0.0.0 最大 ...
- ubuntu 命令、linux环境变量设置
解压与压缩: tar.gz格式tar -xzvf xxx jar格式jar -xvf xxx.jar zip格式unzip xxx.zip zip -r xxx.zip xxx unarunar -e ...
- insufficient privileges for 'SYS' when 'shutdown immediate'
SQL> select * from v$version; BANNER------------------------------------------------------------- ...
- python匿名函数 高阶函数 内置函数 文件操作
1.匿名函数 匿名就是没有名字 def func(x,y,z=1): return x+y+z 匿名 lambda x,y,z=1:x+y+z #与函数有相同的作用域,但是匿名意味着引用计数为0,使用 ...
- nodejs lodash的一些函数
1 _.compact用法 _.compact([0, 1, false, 2, '', 3,'mm']); var test = _.compact([-1,0, 1, false, 2, ...
- 启用优酷html5播放器的办法
方法就是在浏览器中设置下sessionStorage window.sessionStorage.setItem("P_l_h5", true);
- asp.net --- Menu控件\CSS 和样式
几乎 Menu 控件外观的各个方面都可以使用 Menu 控件的属性或级联样式表 (CSS) 来管理.通过了解哪些属性控制呈现的哪些方面,可以定制菜单的外观.本主题介绍由 Menu 控件公开的样式类型, ...