首页 > 代码库 > 210. Course Schedule II
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 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.
- You may assume that there are no duplicate edges in the input prerequisites.
click to show more 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.
链接:https://leetcode.com/problems/course-schedule-ii/#/description
5/13/2017
算法班
BFS
69ms, 9%
indegree保证了每个课程只加1遍,并且是只在前序课程都完成之后加
这道题里可以不使用map,而使用int array作为indegree
1 public class Solution { 2 public int[] findOrder(int numCourses, int[][] prerequisites) { 3 if (prerequisites == null || prerequisites.length == 0) { 4 int[] result = new int[numCourses]; 5 for (int i = 0; i < numCourses; i++) { 6 result[i] = i; 7 } 8 return result; 9 } 10 ArrayList<Integer> ret = new ArrayList<Integer>(); 11 12 Map<Integer, Integer> indegree = getIndegree(numCourses, prerequisites); 13 Queue<Integer> queue = new LinkedList<Integer>(); 14 15 for (int i = 0; i < numCourses; i++) { 16 if (indegree.get(i) == 0) { 17 queue.offer(i); 18 } 19 } 20 while (!queue.isEmpty()) { 21 int course = queue.poll(); 22 for (int i = 0; i < prerequisites.length; i++) { 23 int out = prerequisites[i][0]; 24 int in = prerequisites[i][1]; 25 if (in == course) { 26 int newDegree = indegree.get(out) - 1; 27 indegree.put(out, newDegree); 28 if (newDegree == 0) { 29 queue.offer(out); 30 } 31 } 32 } 33 ret.add(course); 34 } 35 System.out.println(ret.size()); 36 if (ret.size() == numCourses) { 37 int[] result = new int[ret.size()]; 38 for (int i = 0; i < ret.size(); i++) { 39 result[i] = ret.get(i); 40 } 41 return result; 42 } 43 return new int[0]; 44 } 45 46 private Map<Integer, Integer> getIndegree(int numCourses, int[][] prerequisites) { 47 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 48 for (int i = 0; i < numCourses; i++) { 49 map.put(i, 0); 50 } 51 for (int i = 0; i < prerequisites.length; i++) { 52 int out = prerequisites[i][0]; 53 int in = prerequisites[i][1]; 54 map.put(out, map.get(out) + 1); 55 } 56 return map; 57 } 58 }
Kahn‘s Algorithm和Tarjan‘s Algorithm?
还需要研究一下DFS的解法
别人的解释
https://discuss.leetcode.com/topic/13873/two-ac-solution-in-java-using-bfs-and-dfs-with-explanation
https://discuss.leetcode.com/topic/17276/20-lines-c-bfs-dfs-solutions
更多讨论
https://discuss.leetcode.com/category/218/course-schedule-ii
210. Course Schedule II