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. Sql Server中日期时间格式化为字符串输出

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  2. 简单步骤Centos7安装Tomcat 8 (yum安装)(ZT)

    原文:https://blog.51cto.com/bestlope/1978114?source=drt 说明: 操作系统:Centos7 系统环境:未安装其他服务 Step 1:更新系统 sudo ...

  3. 网络层——IP报文头介绍

    IP数据包也叫IP报文分组,传输在ISO网络7层结构中的网络层,它由IP报文头和IP报文用户数据组成,IP报文头的长度一般在20到60个字节之间,而一个IP分组的最大长度则不能超过65535个字节.  ...

  4. java axis2 webservice

    http://clq9761.iteye.com/blog/976029/ 这篇文章写的是最全的,也是最好的.无须下一步下一步操作来操作去,写类直接调试,非常好!

  5. Win2012&Win2008双系统启动菜单设置

    电脑最初安装的是XP,后来想升级操作系统,但XP里又有很多常用软件不想重装,于是装了一个Win2008 R2的双系统,安装好2008R2后,系统自动产生一个2008R2的启动菜单,可以选择进入2008 ...

  6. eclipse git 忽略文件

    ps:git中只有.gitignore文件需要先加索引再提交,其它的都可以直接提交

  7. 学JS的心路历程-物件与原型(一)

    前两天说明面向对象的三大特性及JS不符合面向对象,只能称作支持面向对象而已,今天我们来看看JS的原型继承. 首先我们先来看,什么是原型(vmwork): 两个物件之间的原型关系(prototype r ...

  8. SpringBoot @Async注解失效分析

    有时候在使用的过程中@Async注解会失效(原因和@Transactional注解有时候会失效的原因一样). 下面定义一个Service: 两个异步执行的方法test03()和test02()用来模拟 ...

  9. CSS中的通用字体

    往往设计师在设计时一厢情愿地去思考问题,那样会带来很多潜在的麻烦.事实上你可以为网页设计任意字体,包括艺术字体.也许你的计算机中已经安装了很多字体,但是不能够保证所有的浏览者都已经安装了相同的字体,因 ...

  10. vue生命周期简介

    vue生命周期简介 生命周期的钩子 LifeCycle hooks 上面已经能够清晰的看到vue2.0都包含了哪些生命周期的钩子函数~~ 那么 执行顺序以及什么时候执行,我们上代码来看~~~ 生命周期 ...