Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1.node

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

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?app

For example:ide

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

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

click to show more hints.spa

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。 code

時間複雜度是O(n^2) n爲node的個數blog

空間複雜度是O(e) e 爲depend 的個數(即: 邊數)ip

 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 }
相關文章
相關標籤/搜索