4 - BFS & Topological Algorithm
615. Course Schedule
https://www.lintcode.com/problem/course-schedule/description?_from=ladder&&fromId=1
为什么不可以用 for(int[] prerequisite: prerequisites) 呢 (使用Leetcode上的方法)?因为当 prerequisites 的 length 很大的时候,会超时。所以要定义一个edges[]变量,用来记录每一门课程的后续课程。
要注意数据类型的转换:
int pointer = (int)edges[curr].get(i);
indegree[pointer]--;
public class Solution {
/*
* @param numCourses: a total of n courses
* @param prerequisites: a list of prerequisite pairs
* @return: true if can finish all courses or false
*/
public boolean canFinish(int numCourses, int[][] prerequisites) {
// write your code here
if(prerequisites == null || prerequisites.length == 0 || prerequisites[0].length == 0) {
return true;
}
List[] edges = new List[numCourses];
int[] indegree = new int[numCourses];
int count = 0;
Queue<Integer> queue = new LinkedList<>();
for(int i = 0; i < numCourses; i++) {
edges[i] = new LinkedList<>();
}
int len = prerequisites.length;
for(int i = 0; i < len; i++) {
indegree[prerequisites[i][0]]++;
edges[prerequisites[i][1]].add(prerequisites[i][0]);
}
for(int i = 0; i < numCourses; i++) {
if(indegree[i] == 0) {
queue.offer(i);
}
}
while(!queue.isEmpty()) {
int curr = queue.poll();
count++;
int n = edges[curr].size();
for(int i = 0; i < n; i++) {
int pointer = (int)edges[curr].get(i);
indegree[pointer]--;
if(indegree[pointer] == 0) {
queue.offer(pointer);
}
}
}
return count == numCourses;
}
}
616. Course Schedule II
https://www.lintcode.com/problem/course-schedule-ii/description?_from=ladder&&fromId=1
public class Solution {
/*
* @param numCourses: a total of n courses
* @param prerequisites: a list of prerequisite pairs
* @return: the course order
*/
public int[] findOrder(int numCourses, int[][] prerequisites) {
// write your code here
int[] result = new int[numCourses];
List[] edges = new List[numCourses];
int[] indegree = new int[numCourses];
int count = 0;
Queue<Integer> queue = new LinkedList<>();
for(int i = 0; i < numCourses; i++) {
edges[i] = new LinkedList<>();
}
int len = prerequisites.length;
for(int i = 0; i < len; i++) {
indegree[prerequisites[i][0]]++;
edges[prerequisites[i][1]].add(prerequisites[i][0]);
}
for(int i = 0; i < numCourses; i++) {
if(indegree[i] == 0) {
queue.offer(i);
}
}
while(!queue.isEmpty()) {
int curr = queue.poll();
result[count] = curr;
count++;
int n = edges[curr].size();
for(int i = 0; i < n; i++) {
int pointer = (int)edges[curr].get(i);
indegree[pointer]--;
if(indegree[pointer] == 0) {
queue.offer(pointer);
}
}
}
if(count != numCourses) {
return new int[0];
}
return result;
}
}
4 - BFS & Topological Algorithm的更多相关文章
- PAT 天梯杯 L3-008. 喊山 bfs
L3-008. 喊山 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂 ...
- LMAO?
70 weeks to finish TC problems? 2015.4.16 week1 week1~week8:Graph 1.DFS,BFS,Topological sort,Strongl ...
- CSU1321+SPFA
简单题 /* 简单的bfs */ #include<algorithm> #include<iostream> #include<string.h> #includ ...
- bzoj 1085: [SCOI2005]骑士精神
Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士,且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵 ...
- algorithm@ find the shortest path in a graph using BFS
Finding Shortest Paths By BFS
- Algorithm --> DFS和BFS
定义结点 struct MGraph { int vexs[MAXVEX]; //顶点数组 int arc[MAXVEX][MAXVEX]; //邻接矩阵 int numVertex, numEdge ...
- [Algorithm] BFS vs DFS
//If you know a solution is not far from the root of the tree: BFS, because it is faster to get clos ...
- Robots on a grid(DP+bfs())
链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...
- BFS总结
能够用 BFS 解决的问题,一定不要用 DFS 去做! 因为用 Recursion 实现的 DFS 可能造成 StackOverflow! (NonRecursion 的 DFS 一来你不会写,二来面 ...
随机推荐
- jmeter压测mysql报can not be represented as java.sql.Timestame错误解决方法
JDBC Request 测试mysql时报以下问题? jmeter报错信息: 解决方法: 在数据库url后拼接上字符串?characterEncoding=utf8&zeroDateTim ...
- python可以提高程序执行速度N倍你知道吗?
1.1.Numba的约5分钟指南 Numba是Python的即时编译器,它最适用于使用NumPy数组和函数以及循环的代码.使用Numba的最常用方法是通过其装饰器集合,可以应用于您的函数来指示Numb ...
- openssl源代码结构
Openssl整个软件包主要包括三个主要的功能模块:密码算法库,SSL协议库,应用程序: 应用程序:主要包括密钥生成,证书管理,格式转换,数据加密,签名,SSL测试等. evp,对称算法,非对称算法, ...
- JVM进程启动会启动哪些线程?
首先要明白一点:JVM本身是一个多线程的程序,和我们编写的java应用程序一样,当JVM启动执行时就是在操作系统中启动了一个JVM进程.我们编写的java单线程或多线程应用进程都是在JVM这个程序中作 ...
- 五一培训 DAY1
DAY1 枚举 例题1 题解: 例题2 题解: 例题3 题解: vis[ ]判断是否为素数,pri[ ]储存素数 例题4 题解: 例题5 题解: PS: i < 1<<n ...
- 深入理解 Java 多线程核心知识
多线程相对于其他 Java 知识点来讲,有一定的学习门槛,并且了解起来比较费劲.在平时工作中如若使用不当会出现数据错乱.执行效率低(还不如单线程去运行)或者死锁程序挂掉等等问题,所以掌握了解多线程至关 ...
- json键和值转数组
var jb={"美的":49,"三星":35,"海信":25,"格力":16,"方太":14}; ...
- 2019/4/17 wen 注解、垃圾回收、多线程
- C#线程同步(4)- 通知&EventWaitHandle一家
文章原始出处 http://xxinside.blogbus.com/logs/47523285.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...
- Haystack全文检索
1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch(java写的 ...