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 一来你不会写,二来面 ...
随机推荐
- 架构设计---soa与msa的概念(转)
https://blog.csdn.net/qq_15001229/article/details/79535037
- SSH的软链接后门
之前说过为了防止SSH的后面漏洞 , 升级到高版本的OpenSSH , 那也不能保证万无一失 经典后门 直接对sshd建立软连接 , 之后用任意密码登录即可 看下面操作 创建完软连接后 创建新的会 ...
- Centos7 安装tomcat
- 最短路径 SPFA P3371 【模板】单源最短路径(弱化版)
P3371 [模板]单源最短路径(弱化版) SPFA算法: SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路径,以及判负权环.SPFA 最坏情况下复 ...
- zigbee 中ZDO的理解
---恢复内容开始--- ZigBee 物理层:主要进行无线数据的收发,同时定义了无线传输的信道以及频率. MAC层:使用CSMA-CA机制接入到无线信道,负责传输信标帧,保持同步和 ...
- C#匿名类型和动态解析减少定义传输类模板
C#作为强类型语言,在序列化和反序列化(json)场景中对字符串解析常常需要定义强类型模板,造成编码上的繁琐.其实可以使用匿名类型和动态解析减少json序列化时候的数据模板定义: string a = ...
- python numpy科学计算和数据分析的基础包
import numpy as np #创建ndarray# data1 = [6, 5, 7, 1, 3]# arrl = np.array(data1)# print(arrl)#多维列表创建nd ...
- PHP共享内存yac操作类
http://www.laruence.com/2013/03/18/2846.html 鸟哥介绍 https://www.cnblogs.com/willamwang/p/8918377.htm ...
- [Python数据挖掘]第2章、Python数据分析简介
<Python数据分析与挖掘实战>的数据和代码,可从“泰迪杯”竞赛网站(http://www.tipdm.org/tj/661.jhtml)下载获得 1.Python数据结构 2.Nump ...
- md5 collision(md5碰撞)之记录一些MD5值
md5 collision之记录一些MD5值 “Magic Hash”的PHP漏洞可以使得攻击者非法获取用户的账号信息. 漏洞原因: PHP以一种特定的方式处理被哈希的字符串,攻击者可以利用其 ...