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. springmvc启动时报错:找不到类ContextLoaderListener:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...

  2. spark sql加载avro

    1.spark sql可以直接加载avro文件,之后再进行一系列的操作,示例: SparkConf sparkConf = new SparkConf().setAppName("Spark ...

  3. JSP基本_JavaBeans

    1.JavaBeansとはJavaBeansとは.ある機能を一つにまとめたクラスです.Webアプリケーションでは.JavaBeansは主にデータ操作に使用します.データ管理のプログラムをJavaBea ...

  4. linux 3.10 一次softlock排查

    x86架构.一个同事分析的crash,我在他基础上再次协助分析,也没有获得进展,只是记录一下分析过程.记录是指备忘,万一有人解决过,也好给我们点帮助. 有一次软锁,大多数cpu被锁,log中第一个认为 ...

  5. TLS协议(安全传输层协议)

    概况 安全传输层协议(TLS)用于在两个通信应用程序之间提供保密性和数据完整性.该协议由两层组成: TLS 记录协议(TLS Record)和 TLS 握手协议(TLS Handshake).较低的层 ...

  6. django 认证系统--1

    django的认证系统提供认证和授权两种功能 认证系统包括如下部分: 1.Users 2.Permissions 主要是以 YES/NO 的形式反映一个用户是否能够做某事 3.Groups:就是对多个 ...

  7. C++ 学习 之Struct

    转自https://blog.csdn.net/bestconvenient/article/details/30734139 最开始,就让我们来讨论一下一个最最基本,也最最容易被人忽视掉的问题——C ...

  8. selenium 浏览器常用设置和部署

    一,chrome浏览器设置 from selenium import webdriver # 浏览器选项 chrome_options = webdriver.ChromeOptions() # 使用 ...

  9. sass 的安装 http://blog.csdn.net/weixin_38362146/article/details/78035971?locationNum=10&fps=1

    http://blog.csdn.net/weixin_38362146/article/details/78035971?locationNum=10&fps=1

  10. Using promises

    [Using promises] 过去,异步方法这样写: function successCallback(result) { console.log("It succeeded with ...