Leetcode 207 Course Schedule
2015-06-12 09:11
441 查看
There are a total of n courses you have to take, labeled from
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
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.
拓扑排序,维护入度为0的集合。首先放入所有入度为0的点,逐个引出这个原点所能到达的点并删除原点,如果被到达的点在删除这条原点到其的路径后入度为0,则将这个点放入集合内。
伪代码
更新版本
0to
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.
拓扑排序,维护入度为0的集合。首先放入所有入度为0的点,逐个引出这个原点所能到达的点并删除原点,如果被到达的点在删除这条原点到其的路径后入度为0,则将这个点放入集合内。
伪代码
L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order)
require 'set' def can_finish(num_courses, prerequisites) graph, neighbour = Hash.new{|hsh,key| hsh[key] = Set.new}, Hash.new{|hsh,key| hsh[key] = Set.new} prerequisites.each {|x,y| graph[x] << y; neighbour[y] << x} zero_degree, count = [], 0 num_courses.times {|x| zero_degree << x if graph[x].empty?} while not zero_degree.empty? node = zero_degree.pop count += 1 neighbour[node].each do |x| graph[x] -= [node] zero_degree << x if graph[x].empty? end end count == num_courses end
更新版本
class Solution(object): def canFinish(self, numCourses, prerequisites): indegree, outdegree = {x:[] for x in range(numCourses)}, {x:[] for x in range(numCourses)} for course, preq in prerequisites: indegree[course].append(preq) outdegree[preq].append(course) count, zerodegree = 0, [] for course in range(numCourses): if not indegree[course]: zerodegree.append(course) while zerodegree: node = zerodegree.pop() count += 1 for course in outdegree[node]: indegree[course].remove(node) if not indegree[course]: zerodegree.append(course) return count == numCourses
相关文章推荐
- 随机生成数
- android 录像和拍照功能
- android studio快捷键集合
- Activity的生命周期
- 存储安全:确保变量先定义再使用
- 正则表达式---JS
- Eigen的使用
- Android Studio的设置教程
- 真机调试 限制设备功能
- Java的委托
- ffmpeg 学习笔记
- Redis命令学习—Hash(哈希表)操作
- Android studio 安装中遇到一些问题的解决办法
- 21Hash算法以及暴雪Hash
- 谷歌开发工具Android Studio安装使用图文教程
- Java中的构造函数
- 数字示波器的使用情况
- Webrtc Intro - Native APIs
- 栈溢出崩溃排查(一)
- C#画笔Pen绘制光滑模式曲线的方法