[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 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的更多相关文章
- 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
here are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prere ...
- [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] 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 ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- [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 ...
随机推荐
- Delphi与各数据库数据类型比较
Delphi数据类型与各数据库数据类型对比如下表,如有具体说明见表中脚注: Delphi Type Oracle Types SQL Server Types MySQL Types [1] Inte ...
- Notepad++好用的功能和插件
Notepad++是一款Windows环境下免费开源的代码编辑器,支持Python,shell,Java等主流语言编写.本文主要描述Notepad++一些好用但是容易忽视的功能. 1.根据文件内容查找 ...
- [jQ/PHP]再谈使用JS数组储值的运用(提交PHP处理)
--------------------------------------------------------------------------------------------------- ...
- java实现excel表格导出
Java 实现导出excel表 POI 1.首先下载poi-3.6-20091214.jar,下载地址如下: http://download.csdn.net/detail/evangel_z/389 ...
- Roslyn Cookbook
Roslyn Cookbook by Manish Vasani Publisher: Packt Publishing Release Date: July 2017 ISBN: 978178728 ...
- 消息中间件MQ详解及四大MQ比较
一.消息中间件相关知识 1.概述 消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流 ...
- java.lang.IllegalArgumentException: Missing either @POST URL or @Url parameter.
以前联调的接口,都是类似这样子的http://ip:8080/WLInterface/register 在baseUrl(http://ip:8080/WLInterface/register ) ...
- Spring Boot application.yml bootstrap.yml
yml与properties 其实yml和properties文件是一样的原理,且一个项目上要么yml或者properties,二选一的存在. 推荐使用yml,更简洁. bootstrap与appli ...
- 签名Cookie
[签名Cookie] set-cookie时加上防篡改验证码. 如: user_name=alex|bj95ef23cc6daecc475de 防篡改验证码的生成规则可以很简单:md5(cooki ...
- black-hole《XSS的原理分析与解剖》阅读笔记
0×01 前言: <xss攻击手法>一开始在互联网上资料并不多(都是现成的代码,没有从基础的开始),直到刺的<白帽子讲WEB安全>和cn4rry的<XSS跨站脚本攻击剖析 ...