11/11 <Topological Sort> 207
207. Course Schedule
我们定义二维数组 graph 来表示这个有向图,一维数组 in 来表示每个顶点的入度。我们开始先根据输入来建立这个有向图,并将入度数组也初始化好。然后我们定义一个 queue 变量,将所有入度为0的点放入队列中,然后开始遍历队列,从 graph 里遍历其连接的点,每到达一个新节点,将其入度减一,如果此时该点入度为0,则放入队列末尾。直到遍历完队列中所有的值,若此时还有节点的入度不为0,则说明环存在,返回 false,反之则返回 true
inDegree[]: 修该门课程的入度,即先修课数量;
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] inDegree = new int[numCourses];
Map<Integer, List<Integer>> graph = new HashMap<>();
//Graph: key->课程 value->所需先修课列表
//inDegree[] 下标对应课程,value为对应的入度
for(int i = 0; i < prerequisites.length; i++){
inDegree[prerequisites[i][0]]++;
if(graph.containsKey(prerequisites[i][1])){
graph.get(prerequisites[i][1]).add(prerequisites[i][0]);
}else{
ArrayList<Integer> list = new ArrayList<>();
list.add(prerequisites[i][0]);
graph.put(prerequisites[i][1], list);
}
}
Queue<Integer> queue = new LinkedList<>();
for(int i = 0; i < numCourses; i++){
if(inDegree[i] == 0) queue.add(i);
}
while(!queue.isEmpty()){
int cur = queue.poll();
List<Integer> toTake = graph.get(cur);
for(int i = 0; toTake != null && i < toTake.size(); i++){
inDegree[toTake.get(i)]--;
if(inDegree[toTake.get(i)] == 0) queue.add(toTake.get(i));
}
}
for(int i = 0; i <numCourses; i++){
if(inDegree[i] != 0) return false;
}
return true;
}
}
210. Course Schedule II
用Order[]来存储路线
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
if(numCourses == 0) return null;
int inDegree[] = new int[numCourses], order[] = new int[numCourses], index = 0;
for(int i = 0; i < prerequisites.length; i++){
inDegree[prerequisites[i][0]]++;
}
Queue<Integer> queue = new LinkedList<Integer>();
for(int i = 0; i < numCourses; i++){
if(inDegree[i] == 0){
order[index++] = i;
queue.offer(i);
}
}
while(!queue.isEmpty()){
int cur = queue.poll();
for(int i = 0; i < prerequisites.length; i++){
if(prerequisites[i][1] == cur){
inDegree[prerequisites[i][0]]--;
if(inDegree[prerequisites[i][0]] == 0){
//If inDegree is zero, then add the course to the order
order[index++] = prerequisites[i][0];
queue.offer(prerequisites[i][0]);
}
}
}
}
return (index == numCourses) ? order : new int[0];
}
}
11/11 <Topological Sort> 207的更多相关文章
- NOIp 11.11/12
最后一场比较正式的NOIp模拟赛,写一发小总结.题目没什么好说的,大部分很简单,先贴一下代码. 1111 T1 //string //by Cydiater //2016.11.11 #include ...
- 2021.11.11 EXKMP
2021.11.11 EXKMP https://www.luogu.com.cn/problem/P5410 下标以1开头: #include<cstdio> #include<i ...
- 11.11光棍节工作心得——github/MVP
11.11光棍节工作心得 1.根据scrum meeting thirdday中前辈的指导进行学习 我在博客中贴了链接,竟然TrackBack引来了原博主,
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- topological sort~~~~初学
今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...
- 下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y),y++);
下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...
- topological sort
A topological sortof a dag G is a linear ordering of all its vertices such that if G contains anedg ...
- Hadoop格式化 From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection exception: java.net.
192.168.11.12:8485: Call From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection excep ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
随机推荐
- js json字符串与json对象互相转换(最全)
1.json字符串转json对象 使用场景:通常在取json字符串里具体的值时,会用到. var jsonString = '{"name":"Marydon&quo ...
- LeetCode 133:克隆图 Clone Graph
题目: 给定无向连通图中一个节点的引用,返回该图的深拷贝(克隆).图中的每个节点都包含它的值 val(Int) 和其邻居的列表(list[Node]). Given a reference of a ...
- pixijs shader教程
pixijs 写shader 底层都封装好了 只要改改片段着色器就行了 pxijs一定刚要设置支持透明 不然 颜色不支持透明度了 const app = new PIXI.Application({ ...
- HDU 1723 Distribute Message DP
The contest’s message distribution is a big thing in prepare. Assuming N students stand in a row, fr ...
- Flask--闪现、中间件、多app应用
目录 闪现 源码 案例 中间件 自定义局部中间件 自定义全局装饰器 多app应用 闪现 flask提供了一个非常有用的flash()函数,它可以用来"闪现"需要提示给用户的消息,比 ...
- ASP.NET Core MVC 过滤器
参考网址:https://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_4_3-filters.html ASP.NET Core有五种类型的过滤器,每个过滤 ...
- docker操作命令大全和后台参数
一.命令行 可以通过运行 docker ,或者 docker help 命令得到命令行的帮助信息(我们以 CentOS 为操作环境为例): [root@iz2ze2bn5x2wqxdeq65wlpz ...
- CPU体系结构(组成部分)
在准备网络工程师考试,里面有些知识点是比较常考的.自己写这篇博客呢,当作是笔记吧,自己看一看也分享给大家一起学习. 这部分的内容就是讲CPU里面的组成结构以及各部分的功能. CPU的构成:CPU主要由 ...
- python3装饰器
由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25') ... >> ...
- VMware基本用法
###VMware tools 介绍 只有在VMware虚拟机中安装好了VMware Tools,才能实现主机与虚拟机之间的文件共享,同时可支持自由拖拽的功能,鼠标也可在虚拟机与主机之前自由移动(不用 ...