当前位置:网站首页>Leetcode 207: course schedule (topological sorting determines whether the loop is formed)
Leetcode 207: course schedule (topological sorting determines whether the loop is formed)
2022-06-24 08:01:00 【Swarford】
subject :
Ideas :
notice Dependence problem , First of all, I want to turn the problem into Directed graph !
- Use conditions to build diagrams , Course is the apex , A few nodes have several vertices , Set up in numbers List[ ] Array representation diagram , The index of the array is the value of the vertex , Each element of the array is the adjacency table corresponding to the vertex ;
Use the lessons learned first pre Point to post learning courses follow To represent the order of directed graphs - main Use the first in for Traverse every vertex of the graph , Use... For the adjacency table of vertices dfs
- stay dfs Use the second in for Traverse the adjacency table of the vertex
When onpath True marks isCycle by true
Be careful for Restore after completion onpath=false
Be careful :
For the convenience of memory , When onpath[s]=true and marked[s]=true directly return
Java Realization :
class Solution {
boolean[] marked;
boolean[] onPath;
boolean isCycle=false;
public boolean canFinish(int numCourses, int[][] prerequisites) {
// It's impossible to complete : There is cyclic dependence , Mutual presupposition , You can't finish the course
// Build a diagram
List<Integer>[] graph=buildGraph(numCourses,prerequisites);
// initialization
marked=new boolean[numCourses];
onPath=new boolean[numCourses];
// use dfs Traverse every vertex
for(int i=0;i<numCourses;i++){
dfs(graph,i);
}
return !isCycle;// Learning can be completed without forming a circle
}
List<Integer>[] buildGraph(int numCourses,int[][]prerequisites){
List<Integer>[] graph=new List[numCourses];//graph Elements are arrays !
// Initialize each element :new One List As adjacency table
for(int i=0;i<numCourses;i++){
graph[i]=new LinkedList<>();
}
// Add directed edges
for(int[] k:prerequisites){
int pre=k[1];
int follow=k[0];
graph[pre].add(follow); // graph The index of corresponds to the value of the vertex ,
}
return graph;
}
}
void dfs(List<Integer>[] graph,int s){
// Termination conditions
if(onPath[s]==true){
isCycle=true;
return;
}
if(marked[s]==true){
return;
}
marked[s]=true;// Mark current s
onPath[s]=true;// Mark onpath
// Traverse adjacency table
for(int k:graph[s]){
dfs(graph,k);
}
onPath[s]=false;//onpath Restore
}
边栏推荐
- 1-4metaploitable2 introduction
- Hilbert Huang Transform
- GraphMAE----論文快速閱讀
- 解决 These dependencies were not found: * core-js/modules/es6.array.fill in xxx 之类的问题
- Screenshot recommendation - snipaste
- exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
- decltype用法介绍
- 『C语言』系统日期&时间
- 自动化测试的生命周期是什么?
- Explain the input attribute in PHP (hide the read-only restriction)
猜你喜欢

AWTK 最新动态:Grid 控件新用法

GPU is not used when the code is running

宝塔面板安装php7.2安装phalcon3.3.2

快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning

Chapitre 2: dessiner une fenêtre

第 1 篇:搭建OpenGL环境

没有专业背景,还有机会成为机器学习工程师吗?

Ad-gcl:advantageous graph augmentation to improve graph contractual learning

用Ngrok 配置属于自己的免费外网域名

From jsonpath and XPath to spl
随机推荐
[run the script framework in Django and store the data in the database]
[special session] SME growth plan - ECS special session
毕业两年月薪36k,说难也不难吧
Inline element, block element, inline block element
SCM stm32f103rb, BLDC DC motor controller design, schematic diagram, source code and circuit scheme
3-list introduction
Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
慕思股份在深交所上市:毛利率持续下滑,2022年一季度营销失利
4-operation list (loop structure)
These dependencies were not found: * core JS / modules / es6 array. Fill in XXX
Jenkins is too old try it? Cloud native ci/cd Tekton
常见的数组封装
1-4metaploitable2 introduction
Oracle advanced SQL qualified query
Basics of reptile B1 - scrapy (learning notes of station B)
科一易错点
Leetcode 174 Dungeon games (June 23, 2022)
LeetCode练习——跳跃游戏、组合求和
力扣(LeetCode)174. 地下城游戏(2022.06.23)
Smart pointer remarks