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的更多相关文章

  1. PAT 天梯杯 L3-008. 喊山 bfs

    L3-008. 喊山 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂 ...

  2. LMAO?

    70 weeks to finish TC problems? 2015.4.16 week1 week1~week8:Graph 1.DFS,BFS,Topological sort,Strongl ...

  3. CSU1321+SPFA

    简单题 /* 简单的bfs */ #include<algorithm> #include<iostream> #include<string.h> #includ ...

  4. bzoj 1085: [SCOI2005]骑士精神

    Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士,且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2,纵 ...

  5. algorithm@ find the shortest path in a graph using BFS

    Finding Shortest Paths By BFS

  6. Algorithm --> DFS和BFS

    定义结点 struct MGraph { int vexs[MAXVEX]; //顶点数组 int arc[MAXVEX][MAXVEX]; //邻接矩阵 int numVertex, numEdge ...

  7. [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 ...

  8. 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 ...

  9. BFS总结

    能够用 BFS 解决的问题,一定不要用 DFS 去做! 因为用 Recursion 实现的 DFS 可能造成 StackOverflow! (NonRecursion 的 DFS 一来你不会写,二来面 ...

随机推荐

  1. 如何从零开始系统化学习视觉SLAM?

    由于显示格式问题,建议阅读原文:如何从零开始系统化学习视觉SLAM? 什么是SLAM? SLAM是 Simultaneous Localization And Mapping的 英文首字母组合,一般翻 ...

  2. Ubuntu 常见的问题及常见软件的安装_ubuntu16.04

    1, 在使用 rpm 删除 rpm 包的时候的: rpm -r *** 报错: error reading information on service sfdc3: No such file or ...

  3. JavaScript命名规范基础及系统注意事项

    前端代码中的自定义变量命名           命名方法:     1.驼峰 2.下划线连接           对于文件名,我们一般采用小写字母+下划线的形式     为什么?因为在window下a ...

  4. 关于SimpleDateFormat安全的时间格式化线程安全问题

    想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调 ...

  5. CentOS 7 zabbix添加监控服务器

    CentOS 7 yum安装zabbix 设置中文界面 安装环境 CentOS 7  关闭防火墙和SElinux 在被监控端安装zabbix-agent [root@zabbix-agent ~]# ...

  6. bochs模拟器创建映像文件 、写入文件并启动

    安装 bochs,dd for windows,nasm,并将安装目录加入到环境变量中. 我用的bochs版本是2.6.8 1.用 bochs 中 bximage.exe 创建新的 img 文件 2. ...

  7. Vue系列之 => 使用webpack-dev-server工具实现自动打包编译

    安装webpack-dev-server (webpack版本3.6.0,webpack-dev-server版本2.11.3)注意版本兼容问题,不然会有N多错误. npm i webpack-dev ...

  8. Mysql安装、设置密码、编码

    一.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...

  9. Matlab学以致用--模拟os任务装载情况

    2012-06-08 21:26:42 用matlab来建模,仿真不同时刻os task在队列中的装载情况.输入参数如下 作为初学者,M文件写的有点长.能实现功能就算学以致用了. clear;clc ...

  10. GO值类型与引用类型

    值类型 值类型包括基本数据类型,int,float,bool,string,以及数组和结构体(struct). 值类型变量声明后,不管是否已经赋值,编译器为其分配内存,此时该值存储于栈上. 值类型的默 ...