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 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,所以就不献丑了。



发布了32 篇原创文章 · 获赞 52 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章