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 ...
随机推荐
- CreateFeatureClass COM异常
private static IFeatureClass CreatStnShp(string shp) { //打开工作空间 IWorkspaceFactory wsfactory = new Sh ...
- ICursor查询后的排序问题
为某表做剔除整合处理,各行相关.起初使用 ICursor pCursor = pTable.Search(null,false); 语句查询,ICursor的第一行并没有指向表的第一行,虽然不是第一行 ...
- 支持nmap批量漏洞扫描的script
NSE脚本,会在NMAP扫描时,通过-sV发现的端口详细信息对应到CVE相应的漏洞: nmap -sS -sV --script=vulscan www.scip.ch -p25 下面是该脚本的说明和 ...
- 什么情况下include_path不起作用?
include_path是怎么起作用的? 如果有多个include_path顺序是怎么样的? 什么情况下include_path不起作用? 今天, 我就全面的介绍下这个问题, 先从一个例子开始吧. 如 ...
- [办公自动化]Wlan无法启动,无法连接无线网wifi,所有无线网都搜索不到
转帖: http://support1.lenovo.com.cn/lenovo/wsi/htmls/detail_20121023172943554.html 故障现象: 启动wlan autoco ...
- ecshop session丢失问题
ecshop session丢失问题 电子商务PHP 用ecshop搭建了一个电子商务的系统,本地测试一切正常.放到服务器上出现问题: 症状: 点着点着经常无故退出,感觉session被清空 ...
- PHP程序员必须清楚的问题汇总
PHP程序员必须清楚的问题汇总 投稿:hebedich 字体:[增加 减小] 类型:转载 这篇文章主要介绍了PHP程序员必须清楚的问题汇总,需要的朋友可以参考下 你是否正在准备寻找一份PH ...
- buffer overflow
Computer Systems A Programmer's Perspective Second Edition We have seen that C does not perform any ...
- DML以及DQL的使用方法
DML:数据操作语言 1.插入insert into 单行插入:insert into 表名 (字段名, 字段名,...) values (值, 值, ...) 注:值列表要和字段列表相匹配. ins ...
- mysql和oracle 分页查询(转)
最近简单的对oracle,mysql,sqlserver2005的数据分页查询作了研究,把各自的查询的语句贴出来供大家学习..... (一). mysql的分页查询 mysql的分页查询是最简单的,借 ...