LeetCode 207. Course Schedule(课程安排)
2016-05-04 10:03
429 查看
原题网址:https://leetcode.com/problems/course-schedule/
There are a total of n courses you have to take, labeled from
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
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.
click to show more hints.
Hints:
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.
Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining
the basic concepts of Topological Sort.
Topological sort could also be done via BFS.
方法一:广度优先搜索。
There are a total of n courses you have to take, labeled from
0to
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.
click to show more hints.
Hints:
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.
Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining
the basic concepts of Topological Sort.
Topological sort could also be done via BFS.
方法一:广度优先搜索。
public class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { int[] pre = new int[numCourses]; List<Integer>[] satisfies = new List[numCourses]; for(int i=0; i<numCourses; i++) satisfies[i] = new ArrayList<>(); for(int i=0; i<prerequisites.length; i++) { satisfies[prerequisites[i][1]].add(prerequisites[i][0]); pre[prerequisites[i][0]] ++; } int finish = 0; LinkedList<Integer> queue = new LinkedList<>(); for(int i=0; i<numCourses; i++) { if (pre[i] == 0) queue.add(i); } while (!queue.isEmpty()) { int course = queue.remove(); finish ++; if (satisfies[course] == null) continue; for(int c: satisfies[course]) { pre[c] --; if (pre[c] == 0) queue.add(c); } } return finish == numCourses; } }方法二:深度优先搜索。
public class Solution { private boolean[] canFinish; private boolean[] visited; private List<Integer>[] depends; private boolean canFinish(int course) { if (visited[course]) return canFinish[course]; visited[course] = true; for(int c: depends[course]) { if (!canFinish(c)) return false; } canFinish[course] = true; return canFinish[course]; } public boolean canFinish(int numCourses, int[][] prerequisites) { canFinish = new boolean[numCourses]; visited = new boolean[numCourses]; depends = new List[numCourses]; for(int i=0; i<numCourses; i++) depends[i] = new ArrayList<Integer>(); for(int i=0; i<prerequisites.length; i++) { depends[prerequisites[i][0]].add(prerequisites[i][1]); } for(int i=0; i<numCourses; i++) { if (!canFinish(i)) return false; } return true; } }
相关文章推荐
- Json工具类 - JsonUtils.java
- Retrofit2 完全解析 探索与okhttp之间的关系
- Sql Server知识点系统化Study
- android 之ProgressDialog进度条 显示 单位(kb)
- Android按返回键弹出对话框退出应用程序
- Linux 如何在 vi 里搜索关键字
- winform双击文件打开应用程序
- 华山论剑----常用角点检测与角点匹配方法比较
- C#调用DLL各种传参
- 日期工具类 - DateUtil.java
- Android捕获全局异常CrashExceptionHander 使用
- 归并排序
- 4月学习笔记
- 在html使用a标签 直接下载图片 不通过后台实现直接下载
- iOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- 深入理解CSS变形transform(3d)
- Hibernate.String/LONG 代替
- ViewPager+小圆点
- html笔记
- 加密工具类 - CryptoUtils.java