|
4 | 4 | import java.util.Iterator;
|
5 | 5 | import java.util.Set;
|
6 | 6 |
|
7 |
| -/**There are a total of n courses you have to take, labeled from 0 to n - 1. |
| 7 | +/** |
| 8 | + * 207. Course Schedule |
| 9 | + * |
| 10 | + * There are a total of n courses you have to take, labeled from 0 to n - 1. |
8 | 11 | Some courses may have prerequisites, for example to take course 0 you have to first take course 1,
|
9 | 12 | which is expressed as a pair: [0,1]
|
10 | 13 | Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
|
|
27 | 30 | Hints:
|
28 | 31 | 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.
|
29 | 32 | Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
|
30 |
| - Topological sort could also be done via BFS.*/ |
| 33 | + Topological sort could also be done via BFS. |
| 34 | + */ |
31 | 35 | public class _207 {
|
32 | 36 |
|
33 |
| - public boolean canFinish(int numCourses, int[][] prerequisites) { |
34 |
| - int[] indegree = new int[numCourses]; |
35 |
| - for (int[] prereq : prerequisites) { |
36 |
| - indegree[prereq[0]]++; |
37 |
| - } |
38 |
| - Set<Integer> zeroDegree = new HashSet(); |
39 |
| - for (int i = 0; i < numCourses; i++) { |
40 |
| - if (indegree[i] == 0) { |
41 |
| - zeroDegree.add(i); |
| 37 | + public static class Solution1 { |
| 38 | + public boolean canFinish(int numCourses, int[][] prerequisites) { |
| 39 | + int[] indegree = new int[numCourses]; |
| 40 | + for (int[] prereq : prerequisites) { |
| 41 | + indegree[prereq[0]]++; |
| 42 | + } |
| 43 | + Set<Integer> zeroDegree = new HashSet(); |
| 44 | + for (int i = 0; i < numCourses; i++) { |
| 45 | + if (indegree[i] == 0) { |
| 46 | + zeroDegree.add(i); |
| 47 | + } |
| 48 | + } |
| 49 | + if (zeroDegree.isEmpty()) { |
| 50 | + return false; |
42 | 51 | }
|
43 |
| - } |
44 |
| - if (zeroDegree.isEmpty()) { |
45 |
| - return false; |
46 |
| - } |
47 | 52 |
|
48 |
| - while (!zeroDegree.isEmpty()) { |
49 |
| - Iterator<Integer> it = zeroDegree.iterator(); |
50 |
| - int course = it.next(); |
51 |
| - zeroDegree.remove(course); |
52 |
| - for (int[] prereq : prerequisites) { |
53 |
| - if (prereq[1] == course) { |
54 |
| - indegree[prereq[0]]--; |
55 |
| - if (indegree[prereq[0]] == 0) { |
56 |
| - zeroDegree.add(prereq[0]); |
| 53 | + while (!zeroDegree.isEmpty()) { |
| 54 | + Iterator<Integer> it = zeroDegree.iterator(); |
| 55 | + int course = it.next(); |
| 56 | + zeroDegree.remove(course); |
| 57 | + for (int[] prereq : prerequisites) { |
| 58 | + if (prereq[1] == course) { |
| 59 | + indegree[prereq[0]]--; |
| 60 | + if (indegree[prereq[0]] == 0) { |
| 61 | + zeroDegree.add(prereq[0]); |
| 62 | + } |
57 | 63 | }
|
58 | 64 | }
|
59 | 65 | }
|
60 |
| - } |
61 | 66 |
|
62 |
| - for (int i : indegree) { |
63 |
| - if (i != 0) { |
64 |
| - return false; |
| 67 | + for (int i : indegree) { |
| 68 | + if (i != 0) { |
| 69 | + return false; |
| 70 | + } |
65 | 71 | }
|
| 72 | + return true; |
66 | 73 | }
|
67 |
| - return true; |
68 | 74 | }
|
69 |
| - |
70 | 75 | }
|
0 commit comments