Course Schedule Leetcode

本文介绍了一种通过拓扑排序来解决课程排序问题的方法,并详细解释了如何利用BFS遍历来判断有向图中是否存在环,进而确定是否能完成所有课程。

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:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.
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.

 

这道题是拓扑排序,不过这道题里面其实就是求图里面有没有环。

思路是先用一个二维数组存下所有的对应课程组,然后算出每个点的入度。对于入度为0的课程进行BFS遍历,以0->1为例,如果从0找到1,就把1的入度减一。当1的入度也为0的时候,加入遍历的queue。这样,原则上每个点最后都会被遍历到,遍历的个数应该等于课程的数目。但是如果有环,有的点的入度不能变为0,遍历的个数就会少于课程总数。

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        if (numCourses == 0 || prerequisites == null) {
            return true;
        }
        int[][] matrix = new int[numCourses][numCourses];
        int[] degree = new int[numCourses];
        for (int i = 0; i < prerequisites.length; i++) {
            int pre = prerequisites[i][1];
            int ready = prerequisites[i][0];
            matrix[pre][ready] = 1;
            degree[ready]++;
        }
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if (degree[i] == 0) {
                q.offer(i);
            }
        }
        int count = 0;
        while (!q.isEmpty()) {
            int course = q.poll();
            count++;
            for (int i = 0; i < numCourses; i++) {
                if (matrix[course][i] == 1) {
                    degree[i]--;
                    if (degree[i] == 0) {
                        q.offer(i);
                    }
                }
            }
        }
        return count == numCourses;
    }
}

第一次做这种有向图题目,看了答案。。。到时候还是回顾一下吧。

后来换了arraylist数组做,从50多ms变成了不到10ms,速度提升好大。。。

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        if (numCourses == 0 || prerequisites == null) {
            return true;
        }
        List[] courses = new ArrayList[numCourses];
        // int[][] matrix = new int[numCourses][numCourses];
        int[] degree = new int[numCourses];
        for (int i = 0; i < courses.length; i++) {
            courses[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < prerequisites.length; i++) {
            int pre = prerequisites[i][1];
            int ready = prerequisites[i][0];
            courses[pre].add(ready);
            // matrix[pre][ready] = 1;
            degree[ready]++;
        }
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if (degree[i] == 0) {
                q.offer(i);
            }
        }
        int count = 0;
        while (!q.isEmpty()) {
            int course = q.poll();
            count++;
            for (int i = 0; i < courses[course].size(); i++) {
                int ready = (int) courses[course].get(i);
                degree[ready]--;
                if (degree[ready] == 0) {
                    q.offer(ready);
                }
            }
        }
        return count == numCourses;
    }
}

 

转载于:https://www.cnblogs.com/aprilyang/p/6385862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值