Leetcode: 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.

For example:

2, [[1,0]]
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]

4, [[1,0],[2,0],[3,1],[3,2]]
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].

第二遍做法:Build a graph

 1 public class Solution {
 2     public int[] findOrder(int numCourses, int[][] prerequisites) {
 3         int[] result = new int[numCourses];        
 4         if (numCourses <= 0) return new int[0];
 5         
 6         ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
 7         for (int i=0; i<numCourses; i++) graph.add(new ArrayList<Integer>());
 8         
 9         int len = prerequisites.length;
10         int[] indegree = new int[numCourses];
11         for (int i=0; i<len; i++) {
12             indegree[prerequisites[i][0]]++;
13             graph.get(prerequisites[i][1]).add(prerequisites[i][0]);
14         }
15         Queue<Integer> q = new LinkedList<Integer>();
16         for (int i=0; i<numCourses; i++) {
17             if (indegree[i] == 0) {
18                 q.offer(i);
19             }
20         }
21         int count = 0;
22         while (!q.isEmpty()) {
23             int cur = q.poll();
24             result[count++] = cur;
25             ArrayList<Integer> neibor = graph.get(cur);
26             for (int t=0; t<neibor.size(); t++) {
27                 indegree[neibor.get(t)]--;
28                 if (indegree[neibor.get(t)] == 0) {
29                     q.offer(neibor.get(t));
30                 } 
31             }
32         }
33         //if cycle exists
34         if (count != numCourses) return new int[0];
35         return result;
36     }
37 }

Because we just need one feasible topological sorting of all the courses, we can simply denote each course while we do BFS.

 1 public class Solution {
 2     public int[] findOrder(int numCourses, int[][] prerequisites) {
 3         int[] result = new int[numCourses];        
 4         if (numCourses <= 0) return new int[0];
 5         
 6         int len = prerequisites.length;
 7         int[] countPre = new int[numCourses];
 8         for (int i=0; i<len; i++) {
 9             countPre[prerequisites[i][0]]++;
10         }
11         Queue<Integer> q = new LinkedList<Integer>();
12         for (int i=0; i<numCourses; i++) {
13             if (countPre[i] == 0) {
14                 q.offer(i);
15             }
16         }
17         int count = 0;
18         while (!q.isEmpty()) {
19             int cur = q.poll();
20             result[count++] = cur;
21             for (int i=0; i<len; i++) {
22                 if (prerequisites[i][1] == cur) {
23                     countPre[prerequisites[i][0]]--;
24                     if (countPre[prerequisites[i][0]] == 0) {
25                         q.offer(prerequisites[i][0]);
26                     }
27                 }
28             }
29         }
30         //if cycle exists
31         if (count != numCourses) return new int[0];
32         return result;
33     }
34 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/5050779.html