Skip to content

Commit b29bf6b

Browse files
add 210
1 parent 0b57d02 commit b29bf6b

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ _If you like this project, please leave me a star._ ★
12411241
| 213 | [House Robber II](https://leetcode.com/problems/house-robber-ii/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_213.java) | | Medium | DP
12421242
| 212 | [Word Search II](https://leetcode.com/problems/word-search-ii/) | [Solution](../master/src/main/java/com/fishercoder/solutions/WordSearchII.java) | | Hard | Trie
12431243
| 211 | [Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_211.java) | | Medium | Trie
1244-
| 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_210.java) | | Medium |
1244+
| 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_210.java) | | Medium | Adjacency List, BFS, Topological Sort
12451245
| 209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_209.java) | | Medium |
12461246
| 208 | [Implement Trie](https://leetcode.com/problems/implement-trie-prefix-tree/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_208.java) | [:tv:](https://youtu.be/Br7Wt4V5o1c) | Medium | Trie
12471247
| 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_207.java) | | Medium |

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

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
public class _210 {
6+
public static class Solution1 {
7+
public int[] findOrder(int numCourses, int[][] prerequisites) {
8+
int[] indegree = new int[numCourses];
9+
Map<Integer, List<Integer>> adjacencyList = new HashMap<>();
10+
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+
}
18+
Queue<Integer> q = new LinkedList<>();
19+
for (int i = 0; i < numCourses; i++) {
20+
if (indegree[i] == 0) {
21+
q.offer(i);
22+
}
23+
}
24+
int[] order = new int[numCourses];
25+
int i = 0;
26+
while (!q.isEmpty()) {
27+
Integer course = q.poll();
28+
order[i++] = course;
29+
if (adjacencyList.containsKey(course)) {
30+
for (int neighbor : adjacencyList.get(course)) {
31+
indegree[neighbor]--;
32+
if (indegree[neighbor] == 0) {
33+
q.offer(neighbor);
34+
}
35+
}
36+
}
37+
}
38+
if (i == numCourses) {
39+
return order;
40+
}
41+
return new int[]{};
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)