|
6 | 6 | import java.util.Queue;
|
7 | 7 | import java.util.Set;
|
8 | 8 |
|
9 |
| -/**There are a total of n courses you have to take, labeled from 0 to n - 1. |
| 9 | +/** |
| 10 | + * 210. Course Schedule II |
| 11 | + * |
| 12 | + * There are a total of n courses you have to take, labeled from 0 to n - 1. |
10 | 13 | Some courses may have prerequisites, for example to take course 0 you have to first take course 1,
|
11 | 14 | which is expressed as a pair: [0,1]
|
12 | 15 | Given the total number of courses and a list of prerequisite pairs,
|
|
36 | 39 | Hints:
|
37 | 40 | This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
|
38 | 41 | Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
|
39 |
| - Topological sort could also be done via BFS.*/ |
| 42 | + Topological sort could also be done via BFS. |
| 43 | + */ |
40 | 44 | public class _210 {
|
41 | 45 |
|
42 |
| - public int[] findOrder(int numCourses, int[][] prerequisites) { |
43 |
| - int[] inDegree = new int[numCourses]; |
44 |
| - for (int[] course : prerequisites) { |
45 |
| - inDegree[course[0]]++; |
46 |
| - } |
| 46 | + public static class Solution1 { |
| 47 | + public int[] findOrder(int numCourses, int[][] prerequisites) { |
| 48 | + int[] inDegree = new int[numCourses]; |
| 49 | + for (int[] course : prerequisites) { |
| 50 | + inDegree[course[0]]++; |
| 51 | + } |
47 | 52 |
|
48 |
| - Set<Integer> zeroDegree = new HashSet(); |
49 |
| - Queue<Integer> queue = new LinkedList(); |
50 |
| - for (int i = 0; i < numCourses; i++) { |
51 |
| - if (inDegree[i] == 0) { |
52 |
| - zeroDegree.add(i); |
53 |
| - queue.offer(i); |
| 53 | + Set<Integer> zeroDegree = new HashSet(); |
| 54 | + Queue<Integer> queue = new LinkedList(); |
| 55 | + for (int i = 0; i < numCourses; i++) { |
| 56 | + if (inDegree[i] == 0) { |
| 57 | + zeroDegree.add(i); |
| 58 | + queue.offer(i); |
| 59 | + } |
54 | 60 | }
|
55 |
| - } |
56 | 61 |
|
57 |
| - if (zeroDegree.isEmpty()) { |
58 |
| - return new int[0]; |
59 |
| - } |
| 62 | + if (zeroDegree.isEmpty()) { |
| 63 | + return new int[0]; |
| 64 | + } |
60 | 65 |
|
61 |
| - while (!zeroDegree.isEmpty()) { |
62 |
| - Iterator<Integer> it = zeroDegree.iterator(); |
63 |
| - int course = it.next(); |
64 |
| - zeroDegree.remove(course); |
65 |
| - for (int[] pre : prerequisites) { |
66 |
| - if (course == pre[1]) { |
67 |
| - inDegree[pre[0]]--; |
68 |
| - if (inDegree[pre[0]] == 0) { |
69 |
| - zeroDegree.add(pre[0]); |
70 |
| - queue.offer(pre[0]); |
| 66 | + while (!zeroDegree.isEmpty()) { |
| 67 | + Iterator<Integer> it = zeroDegree.iterator(); |
| 68 | + int course = it.next(); |
| 69 | + zeroDegree.remove(course); |
| 70 | + for (int[] pre : prerequisites) { |
| 71 | + if (course == pre[1]) { |
| 72 | + inDegree[pre[0]]--; |
| 73 | + if (inDegree[pre[0]] == 0) { |
| 74 | + zeroDegree.add(pre[0]); |
| 75 | + queue.offer(pre[0]); |
| 76 | + } |
71 | 77 | }
|
72 | 78 | }
|
73 | 79 | }
|
74 |
| - } |
75 | 80 |
|
76 |
| - for (int i = 0; i < numCourses; i++) { |
77 |
| - if (inDegree[i] != 0) { |
78 |
| - return new int[0]; |
| 81 | + for (int i = 0; i < numCourses; i++) { |
| 82 | + if (inDegree[i] != 0) { |
| 83 | + return new int[0]; |
| 84 | + } |
79 | 85 | }
|
80 |
| - } |
81 | 86 |
|
82 |
| - int[] result = new int[queue.size()]; |
83 |
| - int i = 0; |
84 |
| - while (!queue.isEmpty()) { |
85 |
| - result[i++] = queue.poll(); |
| 87 | + int[] result = new int[queue.size()]; |
| 88 | + int i = 0; |
| 89 | + while (!queue.isEmpty()) { |
| 90 | + result[i++] = queue.poll(); |
| 91 | + } |
| 92 | + return result; |
86 | 93 | }
|
87 |
| - return result; |
88 | 94 | }
|
89 |
| - |
90 | 95 | }
|
0 commit comments