leetcode-207. Course Schedule

原创 2016年08月30日 23:15:52

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.
题意解析:

这道题就是说有一堆课程,有的课程有先修课程,比如说修A之前要修B,然后让你判断,能不能修完这些课程。下面的note是说,给你的先修课程是以边列表呈现的,而不是邻接矩阵,我们可以注意到题目给我们的是一个二维数组,也就是说[[1,0]],表示的是修1之前要修0。

这道题是典型的拓扑排序。原理也很简单,在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在)。这些下面的提示其实也告诉你了。DFS或者BFS都可以。DFS代码如下:

public class Solution {
    public boolean canFinish(int numCourses, int[][] prereq) {
        int[] visited = new int[numCourses];
        
        //courses that would have the dependency graph
        List<List<Integer>> courses = new ArrayList<List<Integer>>();
        
        for(int i=0;i<numCourses;i++){
            courses.add(new ArrayList<Integer>());
        }
        
        //add dependencies
        for(int i=0;i< prereq.length;i++){
            // for a course, add all the courses that are dependent on it
            courses.get(prereq[i][1]).add(prereq[i][0]);
        }
        
        for(int i=0;i<numCourses;i++){
            if(visited[i]==0){
                if(!dfs(i,courses,visited)) return false;
            }
        }
        return true;

    }
    
    public boolean dfs(int i,List<List<Integer>> courses, int[]visited){
        visited[i] = 1;
        
        // these are all the courses which are eligible
        //when we take their prerequisite, which is course i
        List<Integer> eligibleCourses = courses.get(i);
        
        for(int j=0;j<eligibleCourses.size();j++){
            int eligibleCourse = eligibleCourses.get(j);
            //it is already visited, during previous dfs call, so its a cycle 
            if(visited[eligibleCourse] == 1) return false;
            if(visited[eligibleCourse] == 2) continue;
               if(!dfs(eligibleCourse,courses,visited)){
                    return false;
                } 
        }
        
        //it is totally complete and no cycle found in its depth first traversal
        visited[i] = 2;
        return true;
        
        
    }
}
这段代码并不是我写的,而是discuss上找到的一个非常高效的算法。我自己写的BFS只打败了70,这个打败了95,所以就不献丑了。



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

相关文章推荐

leetcode 207. Course Schedule

请点击:原题目链接以打开leetcode题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some ...

LeetCode 207. Course Schedule

这段时间课上在讲图论,就在LeetCode上找题做,图这部分有很多算法,最基本的还是DFS和BFS,下面是其中一道关于选课的题。 有n门课程,代号分别为0,1,2……n-1。其中有某些课程需要选过另...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

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] 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】207. Course Schedule

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

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

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

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

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...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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