[leetcode]Course Schedule

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.

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. There are several ways to represent a graph. For example, the input prerequisites is a graph represented by a list of edges.
     Is this graph representation appropriate?
  3. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  4. Topological sort could also be done via BFS.
题意:总共有n门课程需要修改,且编号为从0到n-1, 但有些课程需要预修课程,如要修课程0就要先修课程1记为[0, 1]
给出课程总数,以及其课程预修关系,问你是否可以修完这些课程?

解题思路:拓扑排序, 判断一个图是否为有向无环图。 这里用二维数组来表示课程间的关系,并用一维数组来统计每门课程的预修课程数
public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        //如果无预备课程则返回真
        if(prerequisites.length == 0) return true;
        int [] preCouNum = new int[numCourses]; //统计当前课程需要的预备课程数
        boolean[][] graph = new boolean[numCourses][numCourses]; //记录课程关系图
        int n = numCourses;
        int m = 0;
        //初始化有向图,并统计出度
        for(int i = 0; i < prerequisites.length; i++){
            int r = prerequisites[i][0];
            int c = prerequisites[i][1];
            if(!graph[r][c]){ //避免重复的数对
                graph[r][c] = true;
                preCouNum[r]++;
                m++;
            }
        }
        Queue<Integer> queue = new LinkedList<Integer>(); //无前向引用的课程 即出度为0 可以修完的课程
        for(int i = 0; i < numCourses; i++){
            if(preCouNum[i] == 0){
                  queue.add(i);
            }
        }
        //queue存的是行号  n这里指最多处理的课程数
        while(!queue.isEmpty() && n > 0){
            int i = queue.remove();
            for(int j = 0; j < numCourses; j++){
                if(graph[j][i]){ //将使用该课程作预备课程的 科目置为false表示该课程已修
                    graph[j][i] = false;
                    m--;
                    if(--preCouNum[j] == 0){//如果当前课程出度为0,可以作为能修完的课程 加入队列
                        queue.add(j);
                    }
                }
            }
            n--;
        }
        //m指所有的数对关系均以处理完 则表示可以完成
        return m == 0;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值