[LeetCode][JavaScript]Course Schedule II

本文详细介绍了一个经典的图论问题——课程排序。通过实例演示了如何利用深度优先搜索(DFS)进行拓扑排序,解决课程先修关系的问题。文章提供了一种有效的算法实现方案,并附带JavaScript代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值