210. Course Schedule II

Problem:
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] .
8/19/2018 update:
初始化graph 和degrees太重要了。
1. 避免了在indegree的时候判断有没有edge
2. 避免了在bfs的时候判断graph.get(cur)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        
        int[] degrees = new int[numCourses];
        List<Integer> temp = new ArrayList<>();
        int[] res = new int[numCourses];
        Map<Integer, List<Integer>> graph = new HashMap<>();
       
        // init graph
        for (int n = 0; n < numCourses; n++) {
            graph.put(n, new ArrayList<>());
        }
        // in degree
        // edge[1] -> edge[0]
        for (int[] edge: prerequisites) {
            degrees[edge[0]]++;
            graph.get(edge[1]).add(edge[0]);
        }
        
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if (degrees[i] == 0)
                queue.offer(i);
        }
        
        while (!queue.isEmpty()) {
            int cur = queue.poll();
            temp.add(cur);
            for (int neib: graph.get(cur)) {
                if(--degrees[neib] == 0)
                        queue.offer(neib);
            }    
            
        }
        
        if (temp.size() != numCourses)
            return new int[]{};
        for (int i = 0; i < numCourses; i++) {
            res[i] = temp.get(i);
        }
        return res;
    }
}

------------------------------------------------------------------------------------------------------------------------
Analysis:
Course schedule的马甲题,顺便复习了一下拓扑排序。
Solution:


class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        List<Integer> list = new ArrayList<>();
        int len = numCourses;
        int[] degrees = new int[len];
        int[][] graph = new int[len][len]; // i <- j

        // indegree
        for (int i = 0; i < prerequisites.length; i++) {
            int target = prerequisites[i][0];
            int source = prerequisites[i][1];
            degrees[target]++;
            graph[target][source]++;
        }

        // inqueue
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < len; i++) {
            if (degrees[i] == 0) {
                queue.offer(i);
            }
        }

        // bfs
        int count = 0;
        while (!queue.isEmpty()) {
            int cur = queue.poll();
            list.add(cur);
            count++;
            for (int i = 0; i < len; i++) {
                if (graph[i][cur] == 1) {
                    if (--degrees[i] == 0) {
                        queue.offer(i);
                    }
                }
            }
        }

        if (count != len ) return new int[]{};
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}

评论

此博客中的热门博文

776. Split BST

663. Equal Tree Partition

532. K-diff Pairs in an Array