设为首页 加入收藏

TOP

[LeetCode] Course Schedule II
2015-11-21 01:02:08 来源: 作者: 【 】 浏览:1
Tags:LeetCode Course Schedule

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.

解题思路:

1、首先将边转化成邻接表的形式,然后逐步找那些没有前驱课程的课,若找到一门课没有前驱课,加入结果中,然后在图中删除这条边。

?

class Solution {
public:
    vector
  
    findOrder(int numCourses, vector
   
    >& prerequisites) { vector
    
      result; if(numCourses<=0){ return result; } //将图转化成邻接表的形式。 vector
     
      > graph(numCourses, set
      
       ()); for(int i=prerequisites.size() - 1; i>=0; i--){ graph[prerequisites[i].first].insert(prerequisites[i].second); } bool flag[numCourses]; //标记是否已经加到了result里面了 memset(flag, 0, sizeof(bool) * numCourses); while(result.size()
       
        时间复杂度为O(n^2),比较慢,虽然AC了,但是花了1492ms。
        

?

2、可以采用空间换时间的办法,记录一个反向图,若找到一门课程后,直接删除对应的边,而无需遍历整张图。另外,记录当前还剩下的那些节点。空间加倍,时间为591ms。

?

class Solution {
public:
    vector
         
           findOrder(int numCourses, vector
          
           >& prerequisites) { vector
           
             result; if(numCourses<=0){ return result; } //将图转化成邻接表的形式。 vector
            
             > graph(numCourses, set
             
              ()); vector
              
               > reverseGraph(numCourses, vector
               
                ()); //反向图 for(int i=prerequisites.size() - 1; i>=0; i--){ graph[prerequisites[i].first].insert(prerequisites[i].second); reverseGraph[prerequisites[i].second].push_back(prerequisites[i].first); } set
                
                  leftNode; //还剩下哪些节点 for(int i=0; i
                 
                  ::iterator it=leftNode.begin(); it!=leftNode.end(); it++){ if(graph[*it].empty()){ index = *it; break; } } if(index<0){ break; } result.push_back(index); leftNode.erase(index); //清除所有以该课程为前驱的边 for(int i=0; i
                  
                   

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BestCoder Round #41 -- (A,B) 下一篇C/C++源代码书写规范

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: