Skip to content

Commit 4068e92

Browse files
refactor 207
1 parent c0e8a15 commit 4068e92

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

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

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.util.Iterator;
55
import java.util.Set;
66

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.
811
Some courses may have prerequisites, for example to take course 0 you have to first take course 1,
912
which is expressed as a pair: [0,1]
1013
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
@@ -27,44 +30,46 @@
2730
Hints:
2831
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.
2932
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+
*/
3135
public class _207 {
3236

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;
4251
}
43-
}
44-
if (zeroDegree.isEmpty()) {
45-
return false;
46-
}
4752

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+
}
5763
}
5864
}
5965
}
60-
}
6166

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+
}
6571
}
72+
return true;
6673
}
67-
return true;
6874
}
69-
7075
}

src/test/java/com/fishercoder/_207Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
import static junit.framework.Assert.assertEquals;
88

99
public class _207Test {
10-
private static _207 test;
10+
private static _207.Solution1 test;
1111
private static boolean actual;
1212
private static boolean expected;
1313
private static int[][] prerequisites;
1414
private static int numCourses;
1515

1616
@BeforeClass
1717
public static void setup() {
18-
test = new _207();
18+
test = new _207.Solution1();
1919
}
2020

2121
@Test

0 commit comments

Comments
 (0)