题目

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. AVFoundation自定义拍照

    0.AVCapture  <AVFoundation/AVFoundation.h> 媒体采集需要的几个对象: 1.AVCaptureDevice: 代表抽象的硬件设备(如前置摄像头,后置 ...

  2. 简易漫画网站搭建-漫画喵Server版

    小喵的唠叨话:寒假的时候写了一个漫画爬虫,爬取了好几个漫画,不过一直没有找到合适的漫画阅读的工具.因此最近就试着自己写一个漫画的网站,放在公网上或者局域网里,这样就能随时随地用手机.Pad看漫画了. ...

  3. javascript解析机制、闭包详解

    js解析机制: js代码解析之前会创建一个如下的词法环境对象(仓库):LexicalEnvironment{ } 在扫描js代码时会把: 1.用声明的方式创建的函数的名字: 2.用var定义的变量的名 ...

  4. idea 中设置成公司规范的代码格式

    优雅的编码格式是一个程序员的必备素质. 最近切换到了 idea,想对自己的代码进行格式化的时候希望能自动排版成公司规定的格式,可以做以下设置: 打开 idea 的 preference: 左侧找到 c ...

  5. C中运算符优先级

    总体规则: 特殊运算符>单目运算符>双目运算符>三目运算符>赋值运算符>逗号运算符 只有单目运算符是右结合,其余的均为左结合

  6. LinkedHashSet的概述和使用

    LinkedHashSet的特点: 可以保证怎么存就怎么取 package online.msym.set; import java.util.LinkedHashSet; public class ...

  7. JetBrains套装免费学生授权申请(IntelliJ, WebStorm...)

    IntelliJ作为一款强大的Java开发IDE,售价自然也不会低.但是对于学生或教师来说,JetBrains开发工具免费提供给学生和教师使用.而且取得一次授权后只需要使用相同的 JetBrains ...

  8. 使用MyBatis对数据库中表实现CRUD操作(二)

    一.使用MyBatis对表实现CRUD操作 1.定义sql映射 userMapper.xml <?xml version="1.0" encoding="UTF-8 ...

  9. css常用居中

    对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...

  10. 日历组件的使用,bootstrap-datetimepicker

    官方文档:http://www.bootcss.com/p/bootstrap-datetimepicker/ .html <input name="createdTimeEnd&qu ...