第五周

207. Course Schedule

  • Total Accepted: 73890
  • Total Submissions: 237873
  • Difficulty: Medium
  • Contributors: Admin

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, is it possible for you to finish all courses?

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 it is possible.

2, [[1,0],[0,1]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.   
思路:
将每门课看成图的一个顶点,若要上课程A必须完成课程B,则把它看成是由点A指向点B的一条边,因此构成一个有向图。本题要解决的是各门课之间是否存在矛盾,可以等价为这个有向图中是否有回路。考虑采用DFS的方法,用二维数组matrix存储该有向图的邻接矩阵,同时建立两个初始值为false的布尔数组visited和tags,visited用于DFS中标记访问过的顶点,tags用于记录各个课程顶点是否已利用has_cycle函数判断过有无回路。
代码:
class Solution {
public:
	bool canFinish(int numCourses, vector
       
       
        
        
         
         >& prerequisites) {
		int size = prerequisites.size();
		vector
         
         
          
          
           
           > matrix(numCourses);
		vector
           
           
             visited(numCourses, false); vector 
            
              tags(numCourses, false); for(int i = 0; i < size; i ++){ matrix[prerequisites[i].first].push_back(prerequisites[i].second); } if(has_cycle(numCourses, visited, matrix, tags)) return false; return true; } bool DFS(int n, vector 
             
               &visited, vector 
               
               
                 > &matrix, vector 
                
                  &tags){ tags[n] = true; visited[n] = true; for(int i = 0; i < matrix[n].size(); i ++){ if(visited[matrix[n][i]] == true || DFS(matrix[n][i], visited, matrix, tags)) return true; } visited[n] = false; return false; } bool has_cycle(int numCourses, vector 
                 
                   &visited, vector 
                   
                   
                     > &matrix, vector 
                    
                      &tags){ for(int i = 0; i < numCourses; i ++){ if(tags[i] == false){ if(DFS(i, visited, matrix, tags)) return true; } } return false; } }; 
                     
                    
                   
                  
                 
                
               
              
             
           
          
          
         
         
        
        
       
       

210. Course Schedule II

  • Total Accepted: 55696
  • Total Submissions: 209487
  • Difficulty: Medium
  • Contributor: LeetCode

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:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.
思路:
这一题是上一题的进阶,上面的题只需判断是否有回路,而这一题还需要返回课程顺序(若不矛盾的话)。但是有了上一题的基础,只需在此基础上增加一个用于记录课程顺序的数组order。在判断出现回路的时候清空该数组,否则则将课程序号按序添加到其中。
代码:
class Solution {
public:
    
	vector
           
           
            
             findOrder(int numCourses, vector
            
            
             
             
              
              >& prerequisites) {
		int size = prerequisites.size();
		vector
              
              
               
                order;
		order.clear();
		vector
               
                
                
                  > matrix(numCourses); vector 
                 
                   visited(numCourses, false); vector 
                  
                    tags(numCourses, false); vector 
                   
                     get(numCourses, false); for(int i = 0; i < size; i ++){ matrix[prerequisites[i].first].push_back(prerequisites[i].second); } if(has_order(numCourses, visited, matrix, tags, get, order)) return order; return order; } bool DFS(int n, vector 
                    
                      &visited, vector 
                      
                      
                        > &matrix, vector 
                       
                         &tags, vector 
                        
                          &get, vector 
                         
                           &order){ tags[n] = true; visited[n] = true; for(int i = 0; i < matrix[n].size(); i ++){ if(visited[matrix[n][i]] == true || DFS(matrix[n][i], visited, matrix, tags, get, order)){ order.clear(); get.clear(); return true; } } visited[n] = false; if(get[n] == false) order.push_back(n); get[n] = true; return false; } bool has_order(int numCourses, vector 
                          
                            &visited, vector 
                            
                            
                              > &matrix, vector 
                             
                               &tags, vector 
                              
                                &get, vector 
                               
                                 &order){ for(int i = 0; i < numCourses; i ++){ if(tags[i] == false){ if(DFS(i, visited, matrix, tags, get, order)){ order.clear(); get.clear(); return false; } } } return true; } }; 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
               
              
              
             
             
            
            
           
           


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值