LeetCode 207. Course Schedule(拓扑排序)
题目
There are a total of n courses you have to take, labeled from
0ton - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
[0,1]Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
2, [[1,0]]There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
2, [[1,0],[0,1]]There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
解题思路
- 本质是一个拓扑排序的问题。
- 将各个任务的入度和出度计算并存储起来
- 将入度为零的任务放在一个队列中
- 不断弹出零入度队列,并且维护(删除)弹出任务的出度关系,当维护出度任务时若该任务的入度为零则加入队列,直到队列为空。

class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
# 创建入度出度空字典
inDegree, outDegree = {x:[] for x in range(numCourses)}, {x:[] for x in range(numCourses)} # 存储入度出度关系
for course, preCourse in prerequisites:
inDegree[course].append(preCourse)
outDegree[preCourse].append(course) count, emptyQueue = 0, [] # 将入度为零的任务加入队列
for i in range(numCourses):
if not inDegree.get(i):
emptyQueue.append(i) # 弹出任务,维护任务
while emptyQueue:
node = emptyQueue.pop()
count += 1
for j in outDegree[node]:
inDegree[j].remove(node)
if not inDegree.get(j):
emptyQueue.append(j) return count == numCourses
LeetCode 207. Course Schedule(拓扑排序)的更多相关文章
- [LeetCode] 207. 课程表(拓扑排序,BFS)
题目 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量 ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [LeetCode] 207. Course Schedule 课程安排
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- LeetCode - 207. Course Schedule
207. Course Schedule Problem's Link ---------------------------------------------------------------- ...
- (medium)LeetCode 207.Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [LeetCode] 207. Course Schedule 课程表
题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...
- [LeetCode] 207. Course Schedule 课程清单
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- [leetcode]207. Course Schedule课程表
在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...
随机推荐
- SEO-长尾词与分词技术
长尾关键词与分词技术 长尾关键词:网站非目标关键词,能给网站带来流量的关键词. 例如:主关键词是成都网站建设 那么,跟成都网站建设相关的词,就叫做长尾关键词. 比如:成都网站建设哪里好?成都网 ...
- 使用htmlparse爬虫技术爬取电影网页的全部下载链接
昨天,我们利用webcollector爬虫技术爬取了网易云音乐17万多首歌曲,而且还包括付费的在内,如果时间允许的话,可以获取更多的音乐下来,当然,也有小伙伴留言说这样会降低国人的知识产权保护意识,诚 ...
- Python中闭包、装饰器的概念
1.闭包(Closure)的概念: 内部函数中对enclosing作用域的变量进行引用 1 passline = 60 2 def func(val): 3 print('%x' % id(val)) ...
- 2017java预备作业2
1 安装git • 到Git官网https://www.git-scm.com/ 下载Git客户端 • 安装时选择默认即可. • 安装完成后在桌面的快捷菜单中选择Git Bash Here 或者在开始 ...
- ngrok localhost和http 的转换
得益于老大的教导,今天又接触到一个有意思的东西,希望分享出来,供大家玩耍----“ngrok”: 乍一看还以为是angualar的新玩意,其实不是.这这家伙可以使本地开发的web应用,不用打包上传,也 ...
- C#传递委托给C或C++库报错__对XXX类型的已垃圾回收委托进行了回调
出现的原因: 因为你传给C或C++的委托是局部的.可能传过去之后就被垃圾回收了,再次调用就会异常. 想办法做成全局的就好 public void Play(string url) { _bassStr ...
- static成员是可以被其所在class创建的实例访问!!!
<span style="font-family: Arial, Helvetica, sans-serif; ">关于静态方法以及静态变量的使用就不详细的说了,我就这 ...
- 阿里安卓面试分析: Android应用的闪退(crash)问题跟踪和解析
一:问题描述 闪退(Crash)是客户端程序在运行时遭遇无法处理的异常或错误时而退出应用程序的表现,请从crash发生的原因分类与解决方法.在出现crash后如何捕捉并分析异常这两个问题给出自己 ...
- 读书笔记 effective c++ Item 37 永远不要重新定义继承而来的函数默认参数值
从一开始就让我们简化这次的讨论.你有两类你能够继承的函数:虚函数和非虚函数.然而,重新定义一个非虚函数总是错误的(Item 36),所以我们可以安全的把这个条款的讨论限定在继承带默认参数值的虚函数上. ...
- String 类的实现(2)深度拷贝详解
我们已经知道了浅拷贝存在的问题,即多次析构同一空间.这个问题是类的成员函数引起的,就是前面浅拷贝里相当于编译器自动合成的函数,确切的说,浅拷贝里的问题是由隐士拷贝构造函数和隐士赋值运算符引起的. 拷贝 ...