#leetcode#Course Schedule

来源:互联网 发布:mac删除虚拟机后空间 编辑:程序博客网 时间:2024/05/22 07: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.

click to show more hints.



拓扑排序,思路是 先遍历一边 prerequisites,举例:如果(0, 1)即想上0课程必须先修1课程, 则在拓扑排序中1的顺序先于0, 1 --> 0,创建一个一维数组 indegree 来维护每个 课程的 入度, 如果存在多个(0, 1),duplicates, 对应入度仍然为1,并不增加。 创建一个二维数组matrix[][]来维护对应每个 节点,在哪个点有出度, 再来看上面的例子,(0, 1), 则 matrix[1][0] = 1.

用一个queue把所有indegree为0的点存储起来, int cur = queue。poll(), count++,遍历matrix的第 cur 层, cur对应哪个点有出度, 则对应点的出度-1,如果对应点出度变成0, 放进queue中循环进行♻️,

如果没有环,则最终所有点都会被遍历到

时间复杂度是⌚️O(v^2), 因为对每个queue。poll(), 都需要遍历numCourse个节点

public class Solution {    public boolean canFinish(int numCourses, int[][] prerequisites) {        if(numCourses < 0 || prerequisites == null){            return false;        }                int[] indegree = new int[numCourses];        int[][] matrix = new int[numCourses][numCourses];        for(int i = 0; i < prerequisites.length; i++){            int cur = prerequisites[i][0];            int pre = prerequisites[i][1];                        if(matrix[pre][cur] == 0){                matrix[pre][cur] = 1;                indegree[cur]++ ;            }else{                // do nothing, in case of duplicates.. for example   two (1, 0), (1, 0), indegree of 0 still is one.            }        }                LinkedList<Integer> queue = new LinkedList<>();        for(int j = 0; j < indegree.length; j++){            if(indegree[j] == 0){                queue.offer(j); // push all node with indegree 0 into the queue            }        }                int count = 0;        while(!queue.isEmpty()){            int course = queue.poll();            count++;            for(int i = 0; i < numCourses; i++){                if(matrix[course][i] != 0){                    indegree[i]--;                    if(indegree[i] == 0){                        queue.offer(i);                    }                }                // if(indegree[i] == 0){   will cause dead loop。。。                //     queue.offer(i);                // }            }        }                return count == numCourses;    }}



九章有graph node版本的topological sort

http://www.jiuzhang.com/solutions/topological-sorting/



贴一个DFS的版本, 学习一下

public class Solution {public boolean canFinish(int numCourses, int[][] prerequisites) {    if(prerequisites==null||prerequisites.length==0) return true;    int n=numCourses;    HashSet<Integer>[] graph=new HashSet[n];    for(int i=0;i<n;i++) graph[i]=new HashSet<>();    for(int i=0;i<prerequisites.length;i++){        graph[prerequisites[i][1]].add(prerequisites[i][0]);    }    boolean[] visited=new boolean[n];     boolean[] visiting=new boolean[n];    for(int i=0;i<n;i++){        if(!visited[i]) {            if(!dfs(i,visited,visiting,graph)) return false;        }    }    return true;}public boolean dfs(int i,boolean[] visited,boolean[] visiting, HashSet<Integer>[] graph){    if(visiting[i]) return false;    visiting[i]=true;    for(Integer j:graph[i]){       if(!visited[j]){           if(!dfs(j,visited,visiting,graph)) return false;       }    }    visiting[i]=false;    visited[i]=true;    return true;}
}



1 0
原创粉丝点击