Created
March 24, 2020 18:21
-
-
Save wushbin/99c246a1c2a870d7e03378918f69fcbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int[] findOrder(int numCourses, int[][] prerequisites) { | |
| List<Integer>[] map = new List[numCourses]; | |
| int[] degree = new int[numCourses]; | |
| for (int i = 0; i < numCourses; i++) { | |
| map[i] = new ArrayList<Integer>(); | |
| } | |
| for (int[] pre : prerequisites) { | |
| map[pre[1]].add(pre[0]); | |
| degree[pre[0]] += 1; | |
| } | |
| Queue<Integer> queue = new LinkedList(); | |
| for (int i = 0; i < numCourses; i++) { | |
| if (degree[i] == 0) { | |
| queue.offer(i); | |
| } | |
| } | |
| int[] res = new int[numCourses]; | |
| int count = 0; | |
| while(!queue.isEmpty()) { | |
| int curr = queue.poll(); | |
| res[count++] = curr; | |
| for (int next : map[curr]) { | |
| degree[next] -= 1; | |
| if (degree[next] == 0) { | |
| queue.offer(next); | |
| } | |
| } | |
| } | |
| if (count < numCourses) { | |
| return new int[]{}; | |
| } | |
| return res; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int[] findOrder(int numCourses, int[][] prerequisites) { | |
| List<Integer>[] graph = new List[numCourses]; | |
| for (int i = 0; i < numCourses; i++) { | |
| graph[i] = new ArrayList<>(); | |
| } | |
| int[] degree = new int[numCourses]; | |
| for (int[] pre : prerequisites) { | |
| graph[pre[1]].add(pre[0]); | |
| degree[pre[0]] += 1; | |
| } | |
| List<Integer> result = new ArrayList<>(); | |
| int[] visited = new int[numCourses]; | |
| for (int i = 0; i < numCourses; i++) { | |
| if (degree[i] != 0 || visited[i] == 2) continue; | |
| if (!search(result, visited, graph, i)) { | |
| return new int[]{}; | |
| } | |
| } | |
| if (result.size() != numCourses) { | |
| return new int[]{}; | |
| } | |
| int[] res = new int[numCourses]; | |
| for (int i = 0; i < numCourses; i++) { | |
| res[i] = result.get(numCourses - 1 - i); | |
| } | |
| return res; | |
| } | |
| public boolean search(List<Integer> result, int[] visited, List<Integer>[] graph, int curr) { | |
| if (visited[curr] == 2) { | |
| return true; | |
| } | |
| if (visited[curr] == 1) { | |
| return false; | |
| } | |
| visited[curr] = 1; | |
| for (int next : graph[curr]) { | |
| if (!search(result, visited, graph, next)) { | |
| return false; | |
| } | |
| } | |
| visited[curr] = 2; | |
| result.add(curr); | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment