题目链接:https://leetcode.com/problems/course-schedule-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.
For example:
2, [[1,0]]
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]
4, [[1,0],[2,0],[3,1],[3,2]]
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].
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
click to show more hints.
Hints:
This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
Topological sort could also be done via BFS.
解题思路:
这题的考点是图的遍历,包括图的广度优先搜索和深度优先搜索,以及拓扑排序。
这题在 207 Course Schedule 的基础上,通过深度优先搜索将先修课程加入到数组中,即得到结果。
需要注意的是,返回空数组的写法是 new int[0],即长度为 0 的数组。
参考链接:http://www.programcreek.com/2014/06/leetcode-course-schedule-ii-java/
代码实现:
public class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
ArrayList<Integer> res = new ArrayList();
//if there is no prerequisites, return a sequence of courses
if(prerequisites.length == 0){
int[] resArray = new int[numCourses];
for(int m = 0; m < numCourses; m ++){
resArray[m]=m;
}
return resArray;
}
if(prerequisites == null || numCourses < 1)
return new int[0];
HashMap<Integer, ArrayList<Integer>> map = new HashMap();
for(int[] a : prerequisites) {
if(!map.containsKey(a[0])) {
ArrayList<Integer> list = new ArrayList();
list.add(a[1]);
map.put(a[0], list);
} else {
map.get(a[0]).add(a[1]);
}
}
int[] visited = new int[numCourses];
for(int i = 0; i < numCourses; i ++) {
if(!GraphDFS(i, map, visited, res))
return new int[0];
}
int[] resArray = new int[numCourses];
for(int i = 0; i < numCourses; i ++) {
resArray[i] = res.get(i);
}
return resArray;
}
boolean GraphDFS(int i, HashMap<Integer, ArrayList<Integer>> map, int[] visited, ArrayList<Integer> res) {
if(visited[i] == -1) // 图中有环
return false;
if(visited[i] == 1) // 该结点之前已被访问过
return true;
visited[i] = -1; // 正在遍历结点k及其后继
if(map.containsKey(i)) {
for(Integer j : map.get(i)) {
if(!GraphDFS(j, map, visited, res))
return false;
}
}
visited[i] = 1; // 该结点及其后继访问完毕
res.add(i);
return true;
}
}
36 / 36 test cases passed.
Status: Accepted
Runtime: 13 ms