[leetcode]210. Course Schedule II课程表II

时间:2021-05-18 06:53:17

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,有资格修哪些课程

[leetcode]210. Course Schedule II课程表II

STEP2: build the map

[leetcode]210. Course Schedule II课程表II

[leetcode]210. Course Schedule II课程表II

[leetcode]210. Course Schedule II课程表II

[leetcode]210. Course Schedule II课程表II

STEP3: topo Sort

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

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

[leetcode]210. Course Schedule II课程表II

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

[leetcode]210. Course Schedule II课程表II

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

[leetcode]210. Course Schedule II课程表II

代码:

 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;
}
}