Skip to content

Commit a21985d

Browse files
refactor 210
1 parent 78de149 commit a21985d

File tree

1 file changed

+43
-38
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+43
-38
lines changed

src/main/java/com/fishercoder/solutions/_210.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.util.Queue;
77
import java.util.Set;
88

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.
1013
Some courses may have prerequisites, for example to take course 0 you have to first take course 1,
1114
which is expressed as a pair: [0,1]
1215
Given the total number of courses and a list of prerequisite pairs,
@@ -36,55 +39,57 @@
3639
Hints:
3740
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.
3841
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+
*/
4044
public class _210 {
4145

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+
}
4752

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+
}
5460
}
55-
}
5661

57-
if (zeroDegree.isEmpty()) {
58-
return new int[0];
59-
}
62+
if (zeroDegree.isEmpty()) {
63+
return new int[0];
64+
}
6065

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+
}
7177
}
7278
}
7379
}
74-
}
7580

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+
}
7985
}
80-
}
8186

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;
8693
}
87-
return result;
8894
}
89-
9095
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy