Leetcode 210 Course Schedule II
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的更多相关文章
- 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 ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
- [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 ...
- [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 ...
- (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 ...
- [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 ...
- 【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 ...
- 【刷题-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 ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
随机推荐
- 《mysql数据库备份小脚本》(转)
vim mysql.sh #!/bin/bashDAY=`date +%Y-%m-%d` //日期以年月日显示并赋予DAY变量SIZE=`du -sh /var/lib/mysql //查看mysql ...
- Echarts - js-20160611
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- wust 1061 链表的合并
怒刷存在感! ~从此wustoj踏上ty博客这样高端霸气上档次的地方啊啦啦~ 只是顺便看了下保研复试题,原来觉得链表好讨厌,现在数据结构学的没办法了,写了个大概是标准的链表合并的写法吧... #inc ...
- Docker+K8S实践
一.运维角度: (一)镜像: 1. 避免依赖过深.不要在基础镜像上加太多产生其他的镜像,我觉得这块最多是三四层. 一层是base景像再往上是工具.中间件这样的,再往上一层就是你自己的程序,再多就比较乱 ...
- Android之网络请求库
自己学习android也有一段时间了,在实际开发中,频繁的接触网络请求,而网络请求的方式很多,最常见的那么几个也就那么几个.本篇文章对常见的网络请求库进行一个总结. HttpUrlConnection ...
- VMware中第一次启动mac遇到的错误及解决方案
本文部分参考 http://blog.sina.com.cn/s/blog_938d86e90100z5ep.html 虚拟机版本:VMware-workstation-full-7.1.3-3242 ...
- Sales Order Flow Statuses
OE_ORDER_LINES_ALL.flow_status_code column values execute the below query to see the values. SELECT ...
- OracleApps 什么是Back to Back Order?
什么是Back to Back Order? 简单的说,B2B是我们从供应商那拿货,然后收到货后,再发运给客户.. B2B Flow B2B的例子 1.Item的定义 Item Should be c ...
- hibernate基础(1)
hibernate基础1.hibernate介绍与动手入门体验 问题:模型不匹配(java对象模型与数据库关系模型不匹配) 解决: 1.使用JDBC手工转换 2.使用ORM(Obje ...
- 第六讲(二) Hibernate HQL查询
HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此Hibe ...