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)为空。
代码:
1 class Solution { 2 public int[] findOrder(int numCourses, int[][] prerequisites) { 3 // Topological sort 4 // Edge case 5 if(numCourses <= 0) return new int[0]; 6 7 //1. Init Map 8 HashMap<Integer, Integer> inDegree = new HashMap<>(); 9 HashMap<Integer, List<Integer>> topoMap = new HashMap<>(); 10 for(int i = 0; i < numCourses; i++) { 11 inDegree.put(i, 0); 12 topoMap.put(i, new ArrayList<Integer>()); 13 } 14 15 //2. Build Map 16 for(int[] pair : prerequisites) { 17 int curCourse = pair[0], preCourse = pair[1]; 18 topoMap.get(preCourse).add(curCourse); // put the child into it's parent's list 19 inDegree.put(curCourse, inDegree.get(curCourse) + 1); // increase child inDegree by 1 20 } 21 //3. find course with 0 indegree, minus one to its children's indegree, until all indegree is 0 22 int[] res = new int[numCourses]; 23 int base = 0; 24 while(!inDegree.isEmpty()) { 25 boolean flag = false; // use to check whether there is cycle 26 for(int key : inDegree.keySet()) { // find nodes with 0 indegree 27 if(inDegree.get(key) == 0) { 28 res[base ++] = key; 29 List<Integer> children = topoMap.get(key); // get the node's children, and minus their inDegree 30 for(int child : children) 31 inDegree.put(child, inDegree.get(child) - 1); 32 inDegree.remove(key); // remove the current node with 0 degree and start over 33 flag = true; 34 break; 35 } 36 } 37 if(!flag) // there is a circle --> All Indegree are not 0 38 return new int[0]; 39 } 40 return res; 41 } 42 }