原题链接在这里: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:

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]

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:

 public class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
List<Integer> res = new ArrayList<Integer>();
List<List<Integer>> graph = new ArrayList<List<Integer>>();
for(int i = 0; i<numCourses; i++){
graph.add(new ArrayList<Integer>());
}
for(int [] edge : prerequisites){
graph.get(edge[1]).add(edge[0]);
} int [] inDegree = new int[numCourses];
for(int [] edge : prerequisites){
inDegree[edge[0]]++;
} LinkedList<Integer> que = new LinkedList<Integer>();
for(int i = 0; i<inDegree.length; i++){
if(inDegree[i] == 0){
que.add(i);
}
} while(!que.isEmpty()){
int source = que.poll();
res.add(source); for(int dest : graph.get(source)){
inDegree[dest]--;
if(inDegree[dest] == 0){
que.add(dest);
}
}
} if(res.size() != numCourses){
return new int[0];
}
int [] resArr = new int[numCourses];
for(int i = 0; i<numCourses; i++){
resArr[i] = res.get(i);
}
return resArr;
}
}

类似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. HttpClient_HttpClient 对 cookie的处理

    session的保持是通过cookie来维持的,所以如果用户有勾选X天内免登录,这个session 就X天内一直有效,就是通过这个cookie来维护.如果没选X天内免登录,基本上就本次才能保持sess ...

  2. 【MVC框架整合】之 SpringMVC3.2.0+MyBatis3.1.1+Spring3.2.0

    1.先整合spring和Mybatis 第一步基本上都是一样加入jar包 创建测试目录 添加junit jar包和log4j配置文件 Log4j的配置文件基本上都是不会变的复制过来就行了 现在就和Hi ...

  3. VirtIE6

    VirtIE6--能在win7上直接运行的单文件 http://down.iefans.net/VirtIE6.rar

  4. 分布式架构高可用架构篇_06_MySQL源码编译安装(CentOS-6.7+MySQL-5.6)

    redhat: 下载:http://dev.mysql.com/downloads/mysql/ 选择5.6 source包 解压 cmake . -DCMAKE_INSTALL_PREFIX=/us ...

  5. Android 通用流行框架大全

    1. 缓存 DiskLruCache    Java实现基于LRU的磁盘缓存 2.图片加载 Android Universal Image Loader 一个强大的加载,缓存,展示图片的库 Picas ...

  6. window.open()弹出窗口防止被禁

    window.open(),顾名思义,是指在当前浏览器窗口弹出另一个浏览器窗口. 因为多种原因,浏览对window.open弹出的窗口做了多方限制.限制不同,肯定会造成各浏览器弹出窗口的差异. 大部分 ...

  7. MySQL出现大量unauthenticated user的问题

    发现这算属MySQL的一个bug,不管连接是通过hosts还是ip的方式,MySQL都会对DNS做反查,IP到DNS,由于反查的接续速度过 慢(不管是不是isp提供的dns服务器的问题或者其他原因), ...

  8. p::first-line { text-transform: uppercase }

    https://www.w3.org/TR/css3-selectors/ Note that the length of the first line depends on a number of ...

  9. IIS7 .NET Runtime version 2.0.50727.5420 - 执行引擎错误(000007FEE77AAF0E) (80131506)

    装完系统,配置完IIS,发现.NET程序报503错误,出错后连接池自动关闭 这个程序是需要连接access数据库的,打开系统日志发现错误如下: 错误应用程序名称: w3wp.exe,版本: 7.5.7 ...

  10. (转)面试题--JAVA中静态块、静态变量加载顺序详解

    public class Test { //1.第一步,准备加载类 public static void main(String[] args) { new Test(); //4.第四步,new一个 ...