关闭

LeetCode 207 Course Schedule

标签: leetcode
111人阅读 评论(0) 收藏 举报
分类:

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.


方法一:题目中所述:对于每个课程,至多会有一个前置课程,以下算法是基于这个基础上写出的,测试用例为int[][] a = {{1, 0}, {2, 3}, {0, 1}},是正确的测试用例。如果为{{1, 0}, {1, 3}, {0, 1}},课程1就有两个前置课程分别为0和3,这不符合题意(即使题意没有特此强调),这种测试用例不在本解答方案的考虑范围内

以下代码AC。Runtime: 1 ms,beats 99.93%.

思路:使用一个数组int[] id = new int[numCourses]并初始化,id[i]=i,如果课程都能读完,那么必然某个课程i没有前置课程,最终id[i]也为i,不会是其它值,否则就会有环,一直循环下去。

	public static boolean canFinish(int numCourses, int[][] prerequisites) {
		int[] id = new int[numCourses];
		for (int i = 0; i < numCourses; i++) id[i] = i;
		for (int[] pair : prerequisites) {
			id[pair[0]] = pair[1];
			if (root(id, pair[0]) ) return false;
		}
		return true;
	}

	private static boolean root(int[] id, int i) {
		int count = 0;
		//如果存在i==id[i],证明以i课程最终的前置课程没有前置,就不会有环
		while (i != id[i]) {
			i = id[i];
			count++;
			if (count == id.length) return false;//存在环,所以会一直循环,导致count=len
		}
		return true;
	}

参考https://discuss.leetcode.com/topic/34846/2ms-java-solution-easy-to-read




方法二:此问题等价于 图(or forest)中有无环的存在. 

此解决方案参考http://www.cnblogs.com/tenosdoit/p/3644225.html的算法3.

此解法见http://blog.csdn.net/xudli/article/details/45860195

	public boolean canFinish2(int numCourses, int[][] prerequisites) {
		int[] map = new int[numCourses];

		for (int i = 0; i < prerequisites.length; i++)
			map[prerequisites[i][1]]++;//记录作为前置课程的course有几个后继,就是点的入度

		Queue<Integer> que = new LinkedList<Integer>();

		for (int i = 0; i < map.length; i++) 
			if (map[i] == 0) que.add(i);//入度为0代表没有别的课程需要先修此课程
		
		int count = que.size();
		while (!que.isEmpty()) {
			int k = que.remove();
			for (int i = 0; i < prerequisites.length; i++) {
				if (k == prerequisites[i][0]) {
					int l = prerequisites[i][1];//k的出度所指向的点,即k需要的先修课程
					map[l]--;//去掉一个入度,等于去掉了一条边
					if (map[l] == 0) {//入度为0的时候,入队,下个循环时寻找此点的先驱
						que.add(l);
						++count;//得到一个入度为0的点,count加1
					}
				}
			}
		}
		return count == numCourses;//所有点的入度最后都为0时,无环
	}

DFS:  http://www.programcreek.com/2014/05/leetcode-course-schedule-java/

https://discuss.leetcode.com/topic/30446/explained-java-12ms-iterative-dfs-solution-based-on-dfs-algorithm-in-clrs

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27817次
    • 积分:2611
    • 等级:
    • 排名:第14478名
    • 原创:241篇
    • 转载:3篇
    • 译文:0篇
    • 评论:17条
    文章分类
    最新评论