LeetCode Course Schedule II
原题链接在这里: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;
}
}
LeetCode Course Schedule II的更多相关文章
- [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 ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
- [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 ...
- 【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 ...
- 【刷题-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 ...
- [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 ...
- [LeetCode] Course Schedule III 课程清单之三
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
随机推荐
- 关于SparkMLlib的基础数据结构 Spark-MLlib-Basics
此部分主要关于MLlib的基础数据结构 1.本地向量 MLlib的本地向量主要分为两种,DenseVector和SparseVector,顾名思义,前者是用来保存稠密向量,后者是用来保存稀疏向量,其创 ...
- 通过ReflectionMethod,我们可以得到Person类的某个方法的信息
PHP反射api由若干类组成,可帮助我们用来访问程序的元数据或者同相关的注释交互.借助反射我们可以获取诸如类实现了那些方法,创建一个类的实例(不同于用new创建),调用一个方法(也不同于常规调用),传 ...
- 用Editplus开发HTML
准备工作 设置Editplus默认的打开浏览器为系统默认浏览器 取消Editplus的临时缓存文件 ☆ 设置不生成临时文件 这里的临时文件,指的是.bak文件,当你在Editplus下修改任意一个文件 ...
- IntelliJ IDEA(社区版)学习记录
一.下载 地址:官网下载地址 二.安装 运行安装程序,一路下一步.注意选择安装路径. 三.基本概念 project:相当于donet下的解决方案 module:相当于donet下的项目工程 四.IDE ...
- var object dynamic的区别
一.var var本身不是一种类型,只是一种语法糖:var声明的变量在赋值的时候即已决定其变量类型,编译时会进行校验. 二.object object是所以类型的基类,故可以赋任何类型的值. 三.dy ...
- [IT扫盲]软件测试时期版本的称呼
有时候搞不懂,还没发布时的软件怎么会就有那么多版本,今天彻底想了解一下. 早有人写好了. 请参考这里: http://baike.baidu.com/view/707808.htm#1_2 测试版 α ...
- 龙珠 超宇宙 [Dragon Ball Xenoverse]
保持了动画气氛实现的新时代的龙珠视觉 今年迎来了[龙珠]系列的30周年,为了把他的魅力最大限度的发挥出来的本作的概念,用最新的技术作出了[2015年版的崭新的龙珠视觉] 在沿袭了一直以来优秀的动画世界 ...
- session配置理解
session.cache_limiter 指定会话页面所使用的缓冲控制方法,默认为nocache.session.cache_expire 以分钟数指定缓冲的会话页面的存活期,默认为180.此设定对 ...
- single-chip microcomputer Microcontroller 单片机 单片微型计算机 微控制器
https://zh.wikipedia.org/wiki/单片机 单片机,全称单片微型计算机(英语:single-chip microcomputer),又称微控制器(microcontroller ...
- 【转】javascript 中that的含义示例介绍
var that = this;,这代表什么意思呢?this代表的是当前对象,var that=this就是将当前的this对象复制一份到that变量中,下面为大家介绍这样做有什么意义 你可能会发现别 ...