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

Example 1:

Input: 2, [[1,0]]
Output: [0,1]
Explanation: 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] .

Example 2:

Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: 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] .

题意: 要修N门课程,给定N门课程之间先修课的规定,求问应该以何种顺序修完N门课。

思路: 拓扑排序。

STEP1:  Init Map

一个Map(indegree) 用来记录对于curCouse,  有多少prerequisites是需要的

一个Map(topoMap)用来记录修完了这门prerequisite,有资格修哪些课程

STEP2: build the map

STEP3: topo Sort

1. 从Map(indegree)中,找到indegree为0的cur-0(表示不需要任何prerequisites), 这门课应该选择先take

2.那么,若修完了这门课,有资格修哪些其他课程呢? 从Map(topoMap)中,找到 pre-0 对应的curList

将curList 中 (1)->(2)->null 每个元素在Map(indegree)的indegree减去1 ( 因为cur-0的course已经take了),即需要的prerequisites少了一门

将cur-0所对应的entrySet从Map(indegree)中删掉,继续在Map(indegree)中寻找indegree为0的cur,直至Map(indegree)为空。

代码:

 class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
// Topological sort
// Edge case
if(numCourses <= 0) return new int[0]; //1. Init Map
HashMap<Integer, Integer> inDegree = new HashMap<>();
HashMap<Integer, List<Integer>> topoMap = new HashMap<>();
for(int i = 0; i < numCourses; i++) {
inDegree.put(i, 0);
topoMap.put(i, new ArrayList<Integer>());
} //2. Build Map
for(int[] pair : prerequisites) {
int curCourse = pair[0], preCourse = pair[1];
topoMap.get(preCourse).add(curCourse); // put the child into it's parent's list
inDegree.put(curCourse, inDegree.get(curCourse) + 1); // increase child inDegree by 1
}
//3. find course with 0 indegree, minus one to its children's indegree, until all indegree is 0
int[] res = new int[numCourses];
int base = 0;
while(!inDegree.isEmpty()) {
boolean flag = false; // use to check whether there is cycle
for(int key : inDegree.keySet()) { // find nodes with 0 indegree
if(inDegree.get(key) == 0) {
res[base ++] = key;
List<Integer> children = topoMap.get(key); // get the node's children, and minus their inDegree
for(int child : children)
inDegree.put(child, inDegree.get(child) - 1);
inDegree.remove(key); // remove the current node with 0 degree and start over
flag = true;
break;
}
}
if(!flag) // there is a circle --> All Indegree are not 0
return new int[0];
}
return res;
}
}
												

[leetcode]210. Course Schedule II课程表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

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

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

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

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

  8. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  9. [LeetCode] Course Schedule I (207) & II (210) 解题思路

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

随机推荐

  1. js弹出div层,弹出层页面底部出现UL出现一条线问题

    整个弹出div层,列表满一页时:底部会出现一条横线 原因:ul固定写在页面中了 解决方法: 将ul代码与li列表一样写在js中,如下 var newhtml = '<ul class=" ...

  2. 多线程中的join总结笔记

    join方法的原理 就是调用相应线程的wait方法进行等待操作的,假如线程1中调用了线程2的join方法,则相当于在线程1中调用了线程2的wait方法,当线程2执行完(或者到达等待时间),线程2会自动 ...

  3. 如何安装和配置RabbitMQ(转载)

    如何安装和配置RabbitMQ 今天开始一个小小的练习,学习一下安装和配置RabbitMQ,为什么要学它,因为WCF可以完全兼容和使用RabbitMQ了.我们新的大数据系统需要使用消息队列,所以就开始 ...

  4. Window环境下Python和Django的安装,以及项目的创建

    1.首先我们要下载python和Django,他们的下载地址如下 python地址:https://www.python.org/ Django地址:  https://www.djangoproje ...

  5. 获取字段唯一值工具- -ArcPy和Python案例学习笔记

    获取字段唯一值工具- -ArcPy和Python案例学习笔记   目的:获取某一字段的唯一值,可以作为工具使用,也可以作为函数调用 联系方式:谢老师,135-4855-4328,xiexiaokui# ...

  6. sublime text3 激活码——许可证

    亲测: 现在是公元2018年6月4日.晴 ``` ----- BEGIN LICENSE ----- sgbteam Single User License EA7E-1153259 8891CBB9 ...

  7. 吴裕雄 oracle 管理数据表对象

  8. Python基础学习Day5 字典的增、删、改、查的用法 分别赋值

    一.字典的介绍 字典:字典是Python的基础数据类型之一:字典可以存储大量数据,关系型数据. 同样是Python中唯一的映射类数据类型.         数据类型的分类:        可变的数据类 ...

  9. linux 内核移植

    1. 下载内核源码linux-2.6.34,解压到工作目录下. 2. 首先在内核中增加一个 SOC ,到 /arch/arm/mach-s3c64xx 下将mach-smdk6410.c 复制成 ma ...

  10. php导出excel不知道列数 php26进制函数

    function num2Letter($num) { $num = intval($num); if ($num <= 0) return false; $letterArr = array( ...