Course Schedule

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, is it possible for you to finish all courses?

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 it is possible.

2, [[1,0],[0,1]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

click to show more hints.

Hints:
  1. This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
  2. There are several ways to represent a graph. For example, the input prerequisites is a graph represented by a list of edges. Is this graph representation appropriate?
  3. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  4. Topological sort could also be done via BFS.

 

这题的重点在于找到不depend on any node的node, 然后一层一层的降depend。 

时间复杂度是O(n^2) n为node的个数

空间复杂度是O(e) e 为depend 的个数(即: 边数)

 1 public class Solution {
 2     public boolean canFinish(int numCourses, int[][] prerequisites) {
 3         if(prerequisites == null || prerequisites.length == 0 || prerequisites[0].length != 2){
 4             return true;
 5         }
 6         int[] depend = new int[numCourses];//node[i] depends on depend[i] # of nodes
 7         //arraylist of node depends on node key
 8         HashMap<Integer, ArrayList<Integer>> mapping = new HashMap<Integer, ArrayList<Integer>>();
 9         for(int i = 0; i < prerequisites.length; i ++){
10             depend[prerequisites[i][0]] ++;
11             if(mapping.containsKey(prerequisites[i][1])){
12                 mapping.get(prerequisites[i][1]).add(prerequisites[i][0]);
13             }else{
14                 ArrayList<Integer> list = new ArrayList<Integer>();
15                 list.add(prerequisites[i][0]);
16                 mapping.put(prerequisites[i][1], list);
17             }
18         }
19         while(mapping.size() != 0){
20             boolean flg = false;
21             for(int i = 0; i < numCourses; i ++){
22                 if(depend[i] == 0 && mapping.containsKey(i)){
23                     //this node doesn't depend on any node, but some node depend on it
24                     flg = true;
25                     for(Integer pos : mapping.get(i)){
26                         depend[pos] --;
27                     }
28                     mapping.remove(i);
29                 }
30             }
31             if(flg == false){
32                 return false;
33             }
34         }
35         return true;
36     }
37 }

 

posted on 2015-05-07 11:55  Step-BY-Step  阅读(197)  评论(0编辑  收藏  举报

导航