Question

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] .

Thinking:

  • Method:拓扑排序
class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[] result = new int[numCourses];
        List<Integer> tempList = new ArrayList<>();
        if(prerequisites == null) return result;
        Map<Integer, List<Integer>> map = new HashMap<>();
        int[] indegree = new int[numCourses];
        for(int i = 0; i < prerequisites.length; i++){
            int first = prerequisites[i][0];
            int second = prerequisites[i][1];
            if(!map.containsKey(second))
                map.put(second, new ArrayList<>());
            map.get(second).add(first);
            indegree[first]++;
        }
        LinkedList<Integer> q = new LinkedList<>();
        for(int i = 0; i < numCourses; i++)
            if(indegree[i] == 0)
                q.add(i);
        while(!q.isEmpty()){
            Integer n = q.poll();
            tempList.add(n);
            if(map.containsKey(n)){
                List<Integer> temp = map.get(n);
                for(Integer t : temp){
                    indegree[t]--;
                    if(indegree[t] == 0){
                        q.add(t);
                    }
                }
            }
        }
        if(tempList.size() != numCourses) return new int[0];
        for(int i = 0; i < numCourses; i++)
            result[i] = tempList.get(i);
        return result;
    }
}

二刷

  1. Method 1: Khan 排序
    class Solution {
     public int[] findOrder(int numCourses, int[][] prerequisites) {
         int[] indegree = new int[numCourses];
         Map<Integer, List<Integer>> pre = new HashMap<>();
         for(int[] p : prerequisites){
             indegree[p[0]]++;
             List<Integer> temp = pre.containsKey(p[1]) ? pre.get(p[1]) : new ArrayList<>();
             temp.add(p[0]);
             pre.put(p[1], temp);
         }
         int[] order = new int[numCourses];
         int count = 0;
         LinkedList<Integer> queue = new LinkedList<>();
         for(int i = 0; i < numCourses; i++){
             if(indegree[i] == 0){
                 queue.add(i);
             }
         }
         while(!queue.isEmpty()){
             int cur = queue.poll();
             order[count++] = cur;
             if(pre.containsKey(cur)){
                 List<Integer> temp = pre.get(cur);
                 for(Integer t : temp){
                     indegree[t]--;
                     if(indegree[t] == 0) queue.add(t);
                 }
             }
         }
         for(int i = 0; i < numCourses; i++){
             if(indegree[i] != 0){
                 return new int[0];
             }
         }
         return order;
     }
    }
    
  2. Method 2: dfs
    class Solution {
     List<Integer> order = new LinkedList<>();
     public int[] findOrder(int numCourses, int[][] prerequisites) {
         int[] indegree = new int[numCourses];
         Map<Integer, List<Integer>> pre = new HashMap<>();
         for(int[] p : prerequisites){
             indegree[p[0]]++;
             List<Integer> temp = pre.containsKey(p[1]) ? pre.get(p[1]) : new ArrayList<>();
             temp.add(p[0]);
             pre.put(p[1], temp);
         }
         boolean[] visited = new boolean[numCourses];
         for(int i = 0; i < numCourses; i++){
             if(!visited[i] && indegree[i] == 0)
                 dfs(i, pre, indegree, visited);
         }
         for(boolean b : visited)
             if(!b) return new int[0];
         int[] orders = new int[numCourses];
         for(int i = 0; i < this.order.size(); i++){
             orders[i] = this.order.get(i);
         }
         return orders;
     }    
     private void dfs(int cur, Map<Integer, List<Integer>> pre, int[] indegree, boolean[] visited){
         visited[cur] = true;
         this.order.add(cur);
         if(pre.containsKey(cur)){
             List<Integer> temp = pre.get(cur);
             for(Integer t :temp){
                 if((--indegree[t]) == 0 && !visited[t]){
                     dfs(t, pre, indegree, visited);
                 }
             }
         }
     }
    }