leecode_210 Course Schedule II

原创 2016年05月31日 04:05:20

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].

想法是先找能修的课(没有要求基础的课),都修了,然后看他们的son,能不能修,能修就丢进queue。

观察那个队列,如果最后队列为空,能修的课的数目小于要求的数目的话,就算规划失败。

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
        
        vector<vector<int>> pre_course;
        vector<vector<int>> next_course;
        vector<int>temp;
        unordered_set<int> set;
        queue<int>  que;
        vector<int> res;
        
        for (int i=0;i<numCourses;i++){
            //next_temp.push_back(temp);
            pre_course.push_back(temp);
            next_course.push_back(temp);
        }
        
        for (int i=0;i<prerequisites.size();i++){
            int from=prerequisites[i].second;
            int to=prerequisites[i].first;
            pre_course[to].push_back(from);
            next_course[from].push_back(to);
        }
        
        for (int i=0;i<numCourses;i++){
            if (pre_course[i].size()==0)
                que.push(i);
            
        }
        
        while(!que.empty()){
            int course_taking=que.front();
            que.pop();
            
            if (set.find(course_taking)==set.end()) {
                  set.insert(course_taking);
                  res.push_back(course_taking);
                  bool all_pre_ok=true;
            
                  for (int i=0;i<next_course[course_taking].size();i++){
                          int next_cour=next_course[course_taking][i];
                          for (int j=0;j<pre_course[next_cour ].size();j++){
                                    if (set.find( pre_course[ next_cour ][j] ) == set.end())
                                              all_pre_ok=false;
                                    
                           }
                  
                         if (all_pre_ok) 
                              que.push(next_course[course_taking][i]);
                              
                         all_pre_ok=true; 
                   }
            
            }
        }
        
        if (res.size()<numCourses)
             res.clear();
        return res;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

210. Course Schedule II\199. Binary Tree Right Side View\279. Perfect Squares

Course Schedule II DESCRIPTION IMPLEMENTATION210. Course Schedule IIDESCRIPTIONThere are a total of ...

207,210. Course Schedule I, II

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have pr...

【LeetCode】210. Course Schedule II

题目: There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have ...

210. Course Schedule II(DFS)

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have pr...

No210. Course Schedule II

一、题目描述 There are a total of n courses you have to take, labeled from 0 to n - 1. Some cour...

LeetCode 210. Course Schedule II(课程安排)

原题网址:https://leetcode.com/problems/course-schedule-ii/ There are a total of n courses you have ...

LeetCode 210. Course Schedule II|图问题.拓扑排序

题目描述There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have p...

LeetCode 210 Course Schedule II

题目描述There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have p...

leetcode - 210. Course Schedule II

算法系列博客之拓扑排序 DFS和入度数组

LeetCode 210. Course Schedule II

ladys and gentleman,又到了令人激动的一周更新时刻,这周我们的主题是拓扑排序,关于拓扑排序请自行度娘,本来昨晚就可以更完的,然而五月天的演唱会吵到我无法思考,甚至短路了,导致昨晚写了...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)