here 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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

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 the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

同 207 Course Schedule

每次取出点的时候放入数组最后输出。

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 do |x,y|
graph[x] << y
neighbour[y] << x
end
zero_degree, count = [], 0
ans =[]
num_courses.times {|x| zero_degree << x if graph[x].empty?}
while not zero_degree.empty?
node = zero_degree.pop
ans << node
count += 1
neighbour[node].each do |x|
graph[x] -= [node]
zero_degree << x if graph[x].empty?
end
end
return ans if count == num_courses
[]
end

Leetcode 210 Course Schedule II的更多相关文章

  1. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  2. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  3. [LeetCode] 210. Course Schedule II 课程清单之二

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  4. [LeetCode] 210. Course Schedule II 课程安排II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  5. (medium)LeetCode 210.Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  6. [leetcode]210. Course Schedule II课程表II

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  7. 【LeetCode】210. Course Schedule II

    Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...

  8. 【刷题-LeetCode】210. Course Schedule II

    Course Schedule II There are a total of n courses you have to take, labeled from 0 to n-1. Some cour ...

  9. [Leetcode Week4]Course Schedule II

    Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...

随机推荐

  1. .NET 压缩解压库发布,支持进度查看

    刚发布了一个 .net压缩解压库,基于SharpZipLib开发 ,支持进度查看,支持路径保持与否. CL.IO.Zip 是一个基于SharpZipLib的一个压缩和解压的类库,提供给用户在.net环 ...

  2. 【用户分析-用户场景】这TM才是产品思维!

    @奶牛Denny :很长一段时间里,市场推广/营销(Marketing)在中国似乎是一个大家很忌讳的词汇.市场推广无非就是夸大包装,炒作一下,卖卖情怀——很多人都是这么觉得的,因为确实有一部分急功近利 ...

  3. Android NDK 和 OpenCV 整合开发总结(3)

    Android NDK 和 OpenCV 整合开发总结(3) http://hujiaweibujidao.github.io/blog/2013/11/18/android-ndk-and-open ...

  4. ThinkPad L421 如何禁用触摸板

    控制面板 - 硬件和声音 - 鼠标 . 选中如下图所示的 UltraNav 选项卡. 将 启用 TouchPad 前的 √ 去掉即可. 如果没有 UltraNav 这一选项卡,可至联想官网下载相关驱动 ...

  5. Ubuntu 12.04 LTS(64bit) 环境下JDK、 Eclipse、 ADT、 快捷图标

    一.在FriendlyARM,Tiny4412,,安装包下可补充: (按照手册添加openjdk-6-jdk 后) 安装JDK (Java),选择需要的JDK,或者全部安装. a) OpenJDK-6 ...

  6. Mac AppStore下载文件的获取

    有时候希望把在mac AppStore下载的App共享给其他人,但是application里面的都是已经安装的应用,那么如何找到pkg安装文件呢? (后附:注意事项!) 方法一: 1.首先下载一个Ap ...

  7. leetcode:Swap Nodes in Pairs

    Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...

  8. linux学习之centos(三):网卡配置

    Linux系统版本:Centos 6.5 在linux学习之centos(二):虚拟网络三种连接方式和SecureCRT的使用中,使用远程工具SecureCRT,通过“ifconfig eth0 + ...

  9. git(icode)分支及发布管理方式

    如果git(icode)不加管理,可能出现枝节蔓生.四处开放的版本库.到处都是分支,完全看不出主干发展的脉络,造成下图的局面: 为了降低合并和版本管理的成本,团队引入一种值得借鉴的管理方式(link) ...

  10. Vim+Ctags+Taglist组合:

    Ctags 1,sudo apt-get install Ctags //会提示最新版本的名字:Exuberant Ctags 2,在源码的最上层目录执行:ctags -R //会在当前目录先生成一个 ...