题目

There are a total of n courses you have to take, labeled from 0 to n - 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:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

解题思路

  1. 本质是一个拓扑排序的问题。
  2. 将各个任务的入度和出度计算并存储起来
  3. 将入度为零的任务放在一个队列中
  4. 不断弹出零入度队列,并且维护(删除)弹出任务的出度关系,当维护出度任务时若该任务的入度为零则加入队列,直到队列为空。

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(拓扑排序)的更多相关文章

  1. [LeetCode] 207. 课程表(拓扑排序,BFS)

    题目 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量 ...

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

  3. [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 ...

  4. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

  5. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  6. (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 ...

  7. [LeetCode] 207. Course Schedule 课程表

    题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...

  8. [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 ...

  9. [leetcode]207. Course Schedule课程表

    在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...

随机推荐

  1. 单源最短路径问题之dijkstra算法

    欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 1. 算法的原理 以源点开始,以源点相连的顶点作为向外延伸的顶点,在所有这些向外延伸的顶 ...

  2. ajax实现分页和分页查询

    之前有写过ajax的加载页面,是非常简单的,而且不需要重新刷新页面,写起来也是非常的方便,今天写的分页是不用封装page.class.php的, 是单纯的js和ajax写出来的  首先为了页面的整齐与 ...

  3. Win7下Nginx的安装与配置,win7nginx配置

    环境介绍:Win7 64位SP1 Nginx版本:nginx/1.8.0 参考链接http://nginx.org/en/docs/windows.html 1.  下载nginx1.8.0版本: h ...

  4. 初用Linux, 安装Ubuntu16.04+NVIDIA387+CUDA8.0+cudnn5.1+TensorFlow1.0.1

    因为最近Deep Learning十分热门, 装一下TensorFlow学习一下. 本文主要介绍安装流程, 将自己遇到的问题说明出来, 并记录自己如何处理, 原理方面并没有能力解释. 由于本人之前从来 ...

  5. Java中实现短信发送

    最近跟着做公司的项目偶然接触到的,顺势把这个给记录下来,给自己梳理一下. 采用引入第三方工具的方式,网上查了半天,发现简单的实现方式便是注册一个中国网建的账号,新建账号的时候会附带赠几条免费短信,彩信 ...

  6. 彻底取消Myeclipse对js文件的校验

    这个是我在网上看到的,自己也做一下笔记. 一般情况下,关掉校验:Window -->Preferences -->MyEclipse -->单击Validation,这样不一定有效. ...

  7. 老李分享:loadrunner用javavuser进行接口测试

    老李分享:loadrunner用javavuser进行接口测试 在这里分享一个poptest培训过程中案例,在日常工作中会遇到被测试系统通讯都是通过加密的数据包,加密算法是公司自己开发的,并且发送的数 ...

  8. MaterialDrawer开源侧滑菜单的使用手册

    官方有详细说明,但是我首次查找的时候并没有第一眼就能使用全部功能,而网上也查找了一下,几乎所有的博客都是简简单单的几句代码...连句说明都没有,可能是我这小菜鸡理解能力不行,或者大神们认为coding ...

  9. Android软键盘遮挡布局的那些事

    有朋友提到软键盘遮挡布局的问题,说网上找了很多资料都未能解决,下面我来总结一下那些事,有些内容也是从网友那里学来的,但是我都会自己验证正确了才会贴出来. 首先来分析下我们常见的遮挡问题有哪些(本次所说 ...

  10. 转接口IC GM7122:BT656转CVBS芯片 视频编码电路

    1 概述    视频编码电路主要实现接收8位CCIR656格式的YUV数据,(例如MPEG解码数据),并编码成CVBS信号,经过D/A转换后输出.基本的编码功能包括副载波产生,色差信号调制,同步信号内 ...