LeetCode 207 Course Schedule

原创 2016年08月31日 00:44:50

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

相关文章推荐

[leetcode] 207.Course Schedule

题目: There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have ...

LeetCode题解(Week5):207. Course Schedule

原题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have pr...

【LeetCode】207. Course Schedule判断有向图是否有环

1、问题描述输入:n, [[1,0], [2,1], …., [n, n-1], [0,1], …..],假设没有重复的边 n表示总共0~n-1门课程,即n个节点 [1,0]表示先上课程0,才能上...

LeetCode 207 - Course Schedule

一、问题描述Description:There are a total of n courses you have to take, labeled from 00 to n−1n-1.Some co...

LeetCode #207 - Course Schedule - Medium

Course Schedule Series Course Schedule I: Course Schedule II: ProblemThere are a total of n...
  • Arcome
  • Arcome
  • 2016年12月21日 20:33
  • 238

[Leetcode] 207. Course Schedule 解题报告

题目: There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may h...

LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)

求有向图中是否有环。 法一:拓扑排序 用一个队列维护所有入度为0的节点,每次弹出一个节点v,查看从v可达的所有节点u; 将u的入读减一,若u的入度此时为0, 则将u加入队列。 在队列为空时,检查所有...

Leetcode 207. Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prere...

[leetcode] 207. Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have...

LeetCode-207/210. Course Schedule (JAVA) (课程安排)

LeetCode-207/210. Course Schedule (JAVA) (课程安排)
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 207 Course Schedule
举报原因:
原因补充:

(最多只允许输入30个字)