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 一来你不会写,二来面 ...
随机推荐
- 短信外部浏览器H5链接一键跳转微信打开任意站
今天讲讲微信跳转的那些事情,这项技术最早出现在在线广告上面,可以从外部引流到微信并打开微信内置浏览器然后打开一个指定的网页地址,在这个网页里面可以放任何想推广的内容,可以是引导文案.活动内容,或者是一 ...
- 《linux就该这么学》第十五节课:第14,15章,dhcp服务和邮件系统
(借鉴请改动) 13章收尾 13.6.分离解析技术 1.在主配置文件中改两个any 2.编辑区域配置文件,写入acl,使用match匹配 ...
- 《linux就该这么学》第十四节课:第13章,部署DNS域名解析服务(bind服务)
(借鉴请改动) 第十二章收尾 12.2.nfs网络文件系统 RHEL7默认安装了nfs,配置文件在 /etc/export 写入格式:共享目录 允许的客户端(参数) ro ...
- IIS部署Angular2
因為Angular無刷新的特性,所以瀏覽器地址欄上的網址其實不會真實映射到磁盤的特定位置,所以我們需要安裝.Rewrite Module, 如下: Web.config 如下: <?xml ve ...
- supervisor 从安装到使用
(转)https://www.jianshu.com/p/3658c963d28b 一.安装 源码安装 先下载最新的supervisor安装包:https://pypi.python.org/pypi ...
- 《高性能Mysql》解读---Mysql的事务和多版本并发
1.base:ACID属性,并发控制 2.MySql事务的隔离级别有哪些,含义是什么? 3.锁知多少,读锁,写锁,排他锁,共享锁,间隙锁,乐观锁,悲观锁. 4.Mysql的事务与锁有什么关联?MySq ...
- Linux环境上部署Flask
[该文章只涉及个人部署的简单流程,读者可通过其它途径了解详细部署流程] 依个人部署项目可预先安装好需要的环境,这里已提前安装好LNMP环境 1.安装Python环境 安装virtualenv环境 配置 ...
- MySql 版本
MySql 版本: netformwork 2.0 netformwork 4.0
- 深入浅出Java探针技术1--基于java agent的字节码增强案例
Java agent又叫做Java 探针,本文将从以下四个问题出发来深入浅出了解下Java agent 一.什么是java agent? Java agent是在JDK1.5引入的,是一种可以动态修改 ...
- Flutter中SQLite数据库的使用
同时支持android和ios 支持事务和批量操作 支持插入/查询/更新/删除操作 在iOS和Android上的后台线程中执行数据库操作 1.添加依赖 dependencies: ... sqflit ...