[LeetCode][JavaScript]Course Schedule II

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:
  1. 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.
  2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  3. Topological sort could also be done via BFS.

https://leetcode.com/submissions/detail/37284494/


 
 
 
紧接着上一题,解法之前已经解释过了:http://www.cnblogs.com/Liok3187/p/4752700.html
最大的区别就是27行和40行的判断。
每轮DFS之前先记录这个点有没有被访问,如果最后没有环,加入结果数组,注意到顺序是反的。
 
 1 /**
 2  * @param {number} numCourses
 3  * @param {number[][]} prerequisites
 4  * @return {number[]}
 5  */
 6 var findOrder = function(numCourses, prerequisites) {
 7     var map = {}, visited = {}, visitedPath = {}, path = [];
 8     //build map
 9     for(i = 0; i < numCourses; i++){
10         map[i] = { index : i, next : [] };
11     }
12     //add edge
13     for(i = 0; i < prerequisites.length; i++){
14         map[prerequisites[i][1]].next.push(map[prerequisites[i][0]]);
15     }
16     //bfs
17     for(i = 0; i < numCourses; i++){
18         if(!visited[i]){
19             if(!bfs(map[i])){
20                 return [];
21             }    
22         }        
23     }
24     return path;
25 
26     function bfs(node){
27         var isVisited = visited[node.index];
28         visited[node.index] = true;
29         visitedPath[node.index] = true;
30         if(node.next.length > 0){
31             for(var i = 0; i < node.next.length; i++){
32                 if(visitedPath[node.next[i].index]){
33                     return false;
34                 } 
35                 if(!bfs(map[node.next[i].index])){
36                     return false;
37                 }
38             }
39         }
40         if(!isVisited){
41            path.unshift(node.index); 
42         }
43         visitedPath[node.index] = false;
44         return true;
45     }
46 };

JS难得可以碾压西加加大法和加瓦大法。

 
 
原文地址:https://www.cnblogs.com/Liok3187/p/4752714.html