#leetcode#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.

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值