第五周

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; } }; 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
               
              
              
             
             
            
            
           
           


内容概要:本文详细探讨了双馈风力发电机(DFIG)在Simulink环境下的建模方法及其在不同风速条件下的电流与电压波形特征。首先介绍了DFIG的基本原理,即定子直接接入电网,转子通过双向变流器连接电网的特点。接着阐述了Simulink模型的具体搭建步骤,包括风力机模型、传动系统模型、DFIG本体模型和变流器模型的建立。文中强调了变流器控制算法的重要性,特别是在应对风速变化时,通过实时调整转子侧的电压和电流,确保电流和电压波形的良好特性。此外,文章还讨论了模型中的关键技术和挑战,如转子电流环控制策略、低电压穿越性能、直流母线电压脉动等问题,并提供了具体的解决方案和技术细节。最终,通过对故障工况的仿真测试,验证了所建模型的有效性和优越性。 适用人群:从事风力发电研究的技术人员、高校相关专业师生、对电力电子控制系统感兴趣的工程技术人员。 使用场景及目标:适用于希望深入了解DFIG工作原理、掌握Simulink建模技能的研究人员;旨在帮助读者理解DFIG在不同风速条件下的动态响应机制,为优化风力发电系统的控制策略提供理论依据和技术支持。 其他说明:文章不仅提供了详细的理论解释,还附有大量Matlab/Simulink代码片段,便于读者进行实践操作。同时,针对一些常见问题给出了实用的调试技巧,有助于提高仿真的准确性和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值