Skip to content

Commit 58e81d0

Browse files
add a solution for 207
1 parent e5cb667 commit 58e81d0

File tree

2 files changed

+56
-0
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+56
-0
lines changed

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_207.java

+51
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,55 @@ private boolean dfs(int course, List<List<Integer>> courseList, int[] visited) {
158158
return true;
159159
}
160160
}
161+
162+
public static class Solution4 {
163+
/**
164+
* This is also Kahn's algorithm, but builds an adjacency list (unncessary for this problem)
165+
* which is often times very helpful in other graph problems, doing it here as a practice:
166+
* it's a very practical template:
167+
* 1. an array of list type to hold all adjacency lists;
168+
* 2. an array of integers to hold indegree for each node;
169+
* 3. several for loops:
170+
* first for-loop: initialize the adjacency list;
171+
* second for-loop: go through all prerequisites to build both adjacency list and indegree array;
172+
* third for-loop: find all nodes that have indegree == zero and add them into the queue;
173+
* 4. start a while loop as long as q is not empty:
174+
* 4.1: poll a node from the q, this node has indegree == zero;
175+
* 4.2: go through all adjacent nodes of this node, decrement ecah of their indegree by one;
176+
* 4.3: if any of their indegree == zero, add that adjacent node into the q
177+
* 5. in the end, all nodes' indegree should be zero, otherwise, it's not a valid topological sortably graph.
178+
*/
179+
public boolean canFinish(int numCourses, int[][] prerequisites) {
180+
List<Integer>[] adjList = new ArrayList[numCourses];
181+
for (int i = 0; i < numCourses; i++) {
182+
adjList[i] = new ArrayList<>();
183+
}
184+
int[] indegree = new int[numCourses];
185+
for (int[] pre : prerequisites) {
186+
indegree[pre[1]]++;
187+
adjList[pre[0]].add(pre[1]);
188+
}
189+
Queue<Integer> q = new LinkedList<>();
190+
for (int i = 0; i < numCourses; i++) {
191+
if (indegree[i] == 0) {
192+
q.offer(i);
193+
}
194+
}
195+
while (!q.isEmpty()) {
196+
Integer curr = q.poll();
197+
for (int v : adjList[curr]) {
198+
indegree[v]--;
199+
if (indegree[v] == 0) {
200+
q.offer(v);
201+
}
202+
}
203+
}
204+
for (int i : indegree) {
205+
if (i != 0) {
206+
return false;
207+
}
208+
}
209+
return true;
210+
}
211+
}
161212
}

Diff for: src/test/java/com/fishercoder/firstthousand/_207Test.java

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class _207Test {
1010
private static _207.Solution1 solution1;
1111
private static _207.Solution2 solution2;
1212
private static _207.Solution3 solution3;
13+
private static _207.Solution4 solution4;
1314
private static int[][] prerequisites;
1415
private static int numCourses;
1516

@@ -18,6 +19,7 @@ public void setup() {
1819
solution1 = new _207.Solution1();
1920
solution2 = new _207.Solution2();
2021
solution3 = new _207.Solution3();
22+
solution4 = new _207.Solution4();
2123
}
2224

2325
@Test
@@ -27,6 +29,7 @@ public void test1() {
2729
assertEquals(true, solution1.canFinish(numCourses, prerequisites));
2830
assertEquals(true, solution2.canFinish(numCourses, prerequisites));
2931
assertEquals(true, solution3.canFinish(numCourses, prerequisites));
32+
assertEquals(true, solution4.canFinish(numCourses, prerequisites));
3033
}
3134

3235
@Test
@@ -47,6 +50,7 @@ public void test2() {
4750
assertEquals(true, solution1.canFinish(numCourses, prerequisites));
4851
assertEquals(true, solution2.canFinish(numCourses, prerequisites));
4952
assertEquals(true, solution3.canFinish(numCourses, prerequisites));
53+
assertEquals(true, solution4.canFinish(numCourses, prerequisites));
5054
}
5155

5256
@Test
@@ -67,5 +71,6 @@ public void test3() {
6771
assertEquals(true, solution1.canFinish(numCourses, prerequisites));
6872
assertEquals(true, solution2.canFinish(numCourses, prerequisites));
6973
assertEquals(true, solution3.canFinish(numCourses, prerequisites));
74+
assertEquals(true, solution4.canFinish(numCourses, prerequisites));
7075
}
7176
}

0 commit comments

Comments
 (0)