原题链接在这里:https://leetcode.com/problems/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:

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

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

题解:

用BFS based topological sort.  若是最后res的size 没有加到 numCourses, 说明没有可行方案,返回空的array.

若是可行,就把res转化成array.

Time Complexity: O(V + E). Space: O(V).

AC Java:

  1. public class Solution {
  2. public int[] findOrder(int numCourses, int[][] prerequisites) {
  3. List<Integer> res = new ArrayList<Integer>();
  4. List<List<Integer>> graph = new ArrayList<List<Integer>>();
  5. for(int i = 0; i<numCourses; i++){
  6. graph.add(new ArrayList<Integer>());
  7. }
  8. for(int [] edge : prerequisites){
  9. graph.get(edge[1]).add(edge[0]);
  10. }
  11.  
  12. int [] inDegree = new int[numCourses];
  13. for(int [] edge : prerequisites){
  14. inDegree[edge[0]]++;
  15. }
  16.  
  17. LinkedList<Integer> que = new LinkedList<Integer>();
  18. for(int i = 0; i<inDegree.length; i++){
  19. if(inDegree[i] == 0){
  20. que.add(i);
  21. }
  22. }
  23.  
  24. while(!que.isEmpty()){
  25. int source = que.poll();
  26. res.add(source);
  27.  
  28. for(int dest : graph.get(source)){
  29. inDegree[dest]--;
  30. if(inDegree[dest] == 0){
  31. que.add(dest);
  32. }
  33. }
  34. }
  35.  
  36. if(res.size() != numCourses){
  37. return new int[0];
  38. }
  39. int [] resArr = new int[numCourses];
  40. for(int i = 0; i<numCourses; i++){
  41. resArr[i] = res.get(i);
  42. }
  43. return resArr;
  44. }
  45. }

类似Course Schedule.

跟上 Course Schedule III.

LeetCode Course Schedule II的更多相关文章

  1. [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 prer ...

  2. [Leetcode Week4]Course Schedule II

    Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...

  3. [LeetCode] Course Schedule I (207) & II (210) 解题思路

    207. Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some ...

  4. 【LeetCode】210. Course Schedule II

    Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...

  5. 【刷题-LeetCode】210. Course Schedule II

    Course Schedule II There are a total of n courses you have to take, labeled from 0 to n-1. Some cour ...

  6. [LeetCode] Course Schedule 课程清单

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

  7. [LeetCode] Course Schedule III 课程清单之三

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  8. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

随机推荐

  1. JSON字符串与java对象的转换

    所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar ...

  2. IIS浏览提示无法显示网页的解决方法

    1.错误号401.1 症状:HTTP 错误 401.1 - 未经授权:访问由于凭据无效被拒绝. 分析: 由于用户匿名访问使用的账号(默认是IUSR_机器名)被禁用,或者没有权限访问计算机,将造成用户无 ...

  3. python中迭代器和生成器

    l=[1,2,3,4] for n in l: print n 在看上面这段代码的时候,我们没有显式的控制列表的偏移量,就可以自动的遍历了整个列表对象.那么for 语句是怎么来遍历列表l的呢?要回答这 ...

  4. 应用服务器上部署自己的 blog 和 wiki 组件。

    协作性应用程序 这就是 Web 2.0 的全部,尽管该术语出现才几乎一年的时间,但现在好像只有烹饪杂志还没有加入到讨论 Web 2.0 未来出路的行列中.自从出现了里程碑式的文章 "What ...

  5. linux进程用户内存空间和内核空间

    When a process running in user mode requests additional memory, pages are allocated from the list of ...

  6. HDU 1003 基础dp 最大连续序列和

    常常做错的一道题.. 因为总是要有一个长度的 所以一开始的s与e都是1 maxx也是a[1] 然后再求 从i=2开始 在这里注意 me永远是当前i 而ms则可能留在原地 可能直接等于i 判断条件就是当 ...

  7. PHP 单例模式代码片段

    <?php error_reporting(E_ALL | E_STRICT); class single{ public $hash; static protected $ins = null ...

  8. GitHub 初探

    前言:有时候在公司上班时自己写了一些代码,打算下班回家后继续写,或者在家修改好的代码第二天要拷到公司继续完善,代码就经常要在这两者之间来回同步,通常情况下我用网盘或者U盘,但是实在是很麻烦,不断的备份 ...

  9. AsyncTask的基本使用

    // String --> doInBackground(Params... params)的参数 // File --> publishProgress(Progress... valu ...

  10. /etc/hosts.conf

    一 作用       指定如何解析主机域名.可设置网络安全. 二 参数说明      默认情况,/etc/hosts.conf 文件有如下内容——      order hosts,bind     ...