[leetcode] 207. Course Schedule

转载 2016年08月29日 08:24:56

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:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

Hints:

  1. This problem is equivalent to finding if a cycle exists 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.

解法一:

BFS 实现topological sort。

class Solution {
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<vector<int>> graph(numCourses, vector<int>(0));
        vector<int> in_degree(numCourses,0);
        
        // initilize in-degree
        for(auto a:prerequisites){
            graph[a.second].push_back(a.first);
            in_degree[a.first]++;
        }
        
        queue<int> q;
        for(int i=0; i<numCourses;i++){
            if(in_degree[i]==0) q.push(i);
        }
        
        while(!q.empty()){
            int k = q.front();
            q.pop();
            for(auto a:graph[k]){
                in_degree[a]--;
                if(in_degree[a]==0) q.push(a);
            }
        }
        
        for(int i=0; i<numCourses; i++){
            if(in_degree[i]!=0) return false;
        }
        return true;
    }
};

解法二:

class Solution {
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<vector<int> > graph(numCourses, vector<int>(0));
        vector<int> visit(numCourses, 0); 
        // visit: 0 has not been visited
        //        1 has been visited
        //       -1 is being visited in current flow
        
        for(auto a:prerequisites){
            graph[a.second].push_back(a.first);
        }
        
        for(int i=0; i<numCourses; i++){
            if(!canFinish(graph, visit, i)) return false;
        }

        return true;
    }
    
    bool canFinish(vector<vector<int> >& graph, vector<int>& visit, int k){
        if (visit[k]==-1) return false;
        if (visit[k]==1) return true;
        
        visit[k]=-1;
        for(auto a:graph[k]){
            if(!canFinish(graph, visit, a)) return false;
        }
        
        visit[k] = 1;
        return true;
    }
};



相关文章推荐

[leetcode] 207.Course Schedule

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

LeetCode题解(Week5):207. Course Schedule

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

【LeetCode】207. Course Schedule判断有向图是否有环

1、问题描述输入:n, [[1,0], [2,1], …., [n, n-1], [0,1], …..],假设没有重复的边 n表示总共0~n-1门课程,即n个节点 [1,0]表示先上课程0,才能上...

LeetCode 207 - Course Schedule

一、问题描述Description:There are a total of n courses you have to take, labeled from 00 to n−1n-1.Some co...

LeetCode #207 - Course Schedule - Medium

Course Schedule Series Course Schedule I: Course Schedule II: ProblemThere are a total of n...
  • Arcome
  • Arcome
  • 2016年12月21日 20:33
  • 239

[Leetcode] 207. Course Schedule 解题报告

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

LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)

求有向图中是否有环。 法一:拓扑排序 用一个队列维护所有入度为0的节点,每次弹出一个节点v,查看从v可达的所有节点u; 将u的入读减一,若u的入度此时为0, 则将u加入队列。 在队列为空时,检查所有...

Leetcode 207. Course Schedule

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

LeetCode-207/210. Course Schedule (JAVA) (课程安排)

LeetCode-207/210. Course Schedule (JAVA) (课程安排)

【Leetcode】207. Course Schedule

Description:There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 207. Course Schedule
举报原因:
原因补充:

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