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:

  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.

click to show more hints.

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.
首先用的是拓扑消除法。用matrix保存所有出度,indegree保存每个课的入度个数。queue弹进所有入度为0的课。如果queue不为空,弹出一个课,count+1,遍历所有出度,如果为1,相应的入度-1,如果某一个入度为零了,立即入栈。最后返回count == nomCourse,即是否消除完所有的入度。代码如下:

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[][] matrix = new int[numCourses][numCourses];
        int[] indegree = new int[numCourses];
        for (int[] pair: prerequisites) {
            indegree[pair[0]]++;
            matrix[pair[1]][pair[0]] = 1;
        }
        Queue<Integer> queue = new LinkedList<Integer>();
        for (int i = 0; i < numCourses; i ++) {
            if (indegree[i] == 0) queue.offer(i);
        }
        int count = 0;
        while (!queue.isEmpty()) {
            int course = queue.poll();
            count ++;
            for (int i = 0; i < numCourses; i ++) {
                if (matrix[course][i] == 1) {
                    if (--indegree[i] == 0) {
                        queue.offer(i);
                    }
                }
            }
        }
        return count == numCourses;
    }
}
另一种方法,深度优先遍历。设置marked标记此序列已经被遍历过,如果被标记过,恰好stacked[course]为true的话说明stack了,loop设置为true,最后返回!loop。代码如下:

public class Solution {
    boolean[] marked;
    boolean[] stacked;
    boolean loop;
    
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        ArrayList[] lists = new ArrayList[numCourses];
        marked = new boolean[numCourses];
        stacked = new boolean[numCourses];
        loop = false;
        for (int i = 0; i < numCourses; i ++) {
            lists[i] = new ArrayList<Integer>();
        }
        for (int[] pair: prerequisites) {
            lists[pair[0]].add(pair[1]);
        }
        for (int i = 0; i < numCourses; i ++) {
            if (!marked[i]) {
                helper(lists, i);
            }
        }
        return !loop;
    }
    
    private void helper(List<Integer>[] lists, int course) {
        stacked[course] = true;
        marked[course] = true;
        if (loop) return;
        for (int i = 0; i < lists[course].size(); i ++) {
            int index = lists[course].get(i);
            if (!marked[index]) {
                helper(lists, index);
            } else if (stacked[index]) {
                loop = true;
            }
        }
        stacked[course] = false;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值