# 210.Course-Schedule-II

## ้ข็ฎๅฐๅ

https://leetcode.com/problems/course-schedule-ii/

## ้ข็ฎๆ่ฟฐ

``````There are a total of n courses you have to take, labeled from 0 to n-1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

Example 1:
Input: 2, [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished  course 0. So the correct course order is [0,1] .

Example 2:
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both  courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.``````

## ไปฃ็ 

### Approach 1: DFS

DFS ้่ฆ visited ๆฐ็ปๆฅๆ ่ฎฐไธ็ง็ถๆ๏ผ

• 0 => ๆช่ฎฟ้ฎ่ฟ

• 1 => ๆญฃๅจ่ฎฟ้ฎ

• 2 => ๅทฒ่ฎฟ้ฎ๏ผๅนถ้่ฟ

``````class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {

Map<Integer, List<Integer>> adjs = new HashMap();
for (int[] pair : prerequisites) {
}

int[] visited = new int[numCourses];
List<Integer> res = new ArrayList<>();
for (int i = 0; i < numCourses; i++) {
if (!topoSort(res, adjs, visited, i)) {
return new int[0];
}
}

int[] result = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
result[i] = res.get(i);
}
return result;
}

private boolean topoSort(List<Integer> res, Map<Integer, List<Integer>> adjs, int[] visited, int i) {

if (visited[i] == 2) {
return true;    // ๆๅบๆๅ
} else if (visited[i] == 1) {
return false; // ๆญฃๅจๆๅบ
}

visited[i] = 1; // ๆญฃๅจๆๅบ
for (int j : adjs.getOrDefault(i, new ArrayList<Integer>())) {
if (topoSort(res, adjs, visited, j) == false) {
return false;
}
}
visited[i] = 2; // ๆๅบๆๅ

return true;
}

}``````

### Approach #2 BFS

BFS ้่ฆ degree ๆฐ็ปๆฅๆ ่ฎฐๅฑๆฐ

• ๆฏๅบ็ฐไธๆฌก degree++

• degree ่ถ้ซไผๅ็บง่ถไฝ

BFS ้ๅไปdgree == 0 ็ๅ็ด ๅผๅง

• ้ๅๆฏไธชๅ็ด ็ไธไธๅฑๅ็ด ๏ผๅฐๅถไธๅฑๅ็ด ็ degree--

• ๅฆๆไธๅฑๅ็ด ๅญๅจ degree == 0, ๅ ๅฅๅฐ results ๆฐ็ปไธญ

• ๅฆๅ๏ผไป queue ไธญ้ๆฐๅๅบๅ็ด ๏ผ่ฟ่ก้ๅ

ๆๅ้่ฆๆฏ่พ็ปๆ results ็้ฟๅบฆๅ n ๆฏๅฆ็ธๅ๏ผ็ธๅๆๆ่งฃ

``````class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {

int[] degree = new int[numCourses];
Map<Integer, List<Integer>> adjs = new HashMap<>();
for (int[] edge : prerequisites) {
degree[edge[0]]++;
}

}

private int[] BFS(int[] degree, Map<Integer, List<Integer>> adjs) {
int[] ans = new int[degree.length];
Queue<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < degree.length; i++) {
if (degree[i] == 0) queue.offer(i);
}

int visited = 0;
while (!queue.isEmpty()) {
int from = queue.poll();
ans[visited++] = from;
for (int to: adjs.getOrDefault(from, new ArrayList<Integer>())) {
degree[to]--;
if (degree[to] == 0) {
queue.offer(to);
}
}
}

return visited == degree.length ? ans : new int[0];
}
}``````

Last updated