Skip to content

Commit a99fc6a

Browse files
add a solution for 210
1 parent 58e81d0 commit a99fc6a

File tree

2 files changed

+80
-7
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+80
-7
lines changed

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

+51-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package com.fishercoder.solutions.firstthousand;
22

3-
import java.util.*;
3+
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Queue;
410

511
public class _210 {
612
public static class Solution1 {
713
public int[] findOrder(int numCourses, int[][] prerequisites) {
814
int[] indegree = new int[numCourses];
915
Map<Integer, List<Integer>> adjacencyList = new HashMap<>();
1016
for (int[] pre : prerequisites) {
11-
int src = pre[1];
12-
int dest = pre[0];
13-
List<Integer> list = adjacencyList.getOrDefault(src, new ArrayList<>());
14-
list.add(dest);
15-
adjacencyList.put(src, list);
16-
indegree[src]++;
17+
List<Integer> list = adjacencyList.getOrDefault(pre[1], new ArrayList<>());
18+
list.add(pre[0]);
19+
adjacencyList.put(pre[1], list);
20+
indegree[pre[0]]++;
1721
}
1822
Queue<Integer> q = new LinkedList<>();
1923
for (int i = 0; i < numCourses; i++) {
@@ -41,4 +45,44 @@ public int[] findOrder(int numCourses, int[][] prerequisites) {
4145
return new int[]{};
4246
}
4347
}
48+
49+
public static class Solution2 {
50+
/**
51+
* Instead of using a map, we can use an array of list type, it turned out to be even faster on LeetCode.
52+
*/
53+
public int[] findOrder(int numCourses, int[][] prerequisites) {
54+
List<Integer>[] adjList = new ArrayList[numCourses];
55+
for (int i = 0; i < numCourses; i++) {
56+
adjList[i] = new ArrayList<>();
57+
}
58+
int[] indegree = new int[numCourses];
59+
for (int[] pre : prerequisites) {
60+
indegree[pre[0]]++;
61+
adjList[pre[1]].add(pre[0]);
62+
}
63+
Queue<Integer> q = new LinkedList<>();
64+
for (int i = 0; i < indegree.length; i++) {
65+
if (indegree[i] == 0) {
66+
q.offer(i);
67+
}
68+
}
69+
int index = 0;
70+
int[] order = new int[numCourses];
71+
while (!q.isEmpty()) {
72+
Integer curr = q.poll();
73+
order[index++] = curr;
74+
for (int v : adjList[curr]) {
75+
indegree[v]--;
76+
if (indegree[v] == 0) {
77+
q.offer(v);
78+
}
79+
}
80+
}
81+
if (index == numCourses) {
82+
return order;
83+
}
84+
return new int[]{};
85+
}
86+
87+
}
4488
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder.firstthousand;
2+
3+
import com.fishercoder.solutions.firstthousand._210;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _210Test {
10+
private static _210.Solution1 solution1;
11+
private static _210.Solution2 solution2;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _210.Solution1();
16+
solution2 = new _210.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertArrayEquals(new int[]{0, 1}, solution1.findOrder(2, new int[][]{
22+
{1, 0}
23+
}));
24+
assertArrayEquals(new int[]{0, 1}, solution2.findOrder(2, new int[][]{
25+
{1, 0}
26+
}));
27+
}
28+
29+
}

0 commit comments

Comments
 (0)