当前位置:网站首页>Lecture 207, Class Schedule
Lecture 207, Class Schedule
2022-08-08 16:04:00 【Ying night snow】
力扣207,课程表
题目描述
你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 .
在选修某些课程之前需要一些先修课程. 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi .
例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 .
请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false .
输入输出样例
输入:numCourses = 2, prerequisites = [[1,0]]
输出:true
解释:总共有 2 门课程.学习课程 1 之前,你需要完成课程 0 .这是可能的.
输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
输出:false
解释:总共有 2 门课程.学习课程 1 之前,你需要先完成课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 .这是不可能的.
tips
1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
prerequisites[i] 中的所有课程对 互不相同
解法一,Use breadth-first search,借用队列
//Determine whether it is an infinite loop
//判断是否存在环
//Think of classes as directed edges
//Use queues to assistBFS
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
//Two tables of degrees and edges are maintained
vector<vector<int>>edges(numCourses,vector<int>());
vector<int>indeg(numCourses);
//Fill data into the degree table and the edge table
for(auto info:prerequisites)
{
edges[info[1]].push_back(info[0]);
indeg[info[0]]++;
}
//Use queues to help,存储入度为0的点
queue<int>que;
for(int i=0;i<numCourses;i++)
{
if(indeg[i]==0)
{
que.push(i);
}
}
//设置标志位,Use and to check if the number of occurrences in the queue is the same as the number of courses to be taken
int visited=0;
while(!que.empty())
{
++visited;
int temp=que.front();
que.pop();
//The edge that will be associated with this dequeued vertex,入度减一
//入度为0的,继续入栈,The loop can be circumvented here
for(auto v:edges[temp])
{
indeg[v]--;
if(indeg[v]==0)
{
que.push(v);
}
}
}
return visited==numCourses;
}
解法二,使用深度优先搜索
//There are three states for saving,未搜索,搜索中,已完成
bool canFinish2(int numCourses, vector<vector<int>>& prerequisites) {
//initialization parameters
vector<vector<int>>edges(numCourses,vector<int>());
vector<int>visited(numCourses);
bool valid=true;
//读取数组,Import data into the edge table
for(auto info:prerequisites)
{
edges[info[1]].push_back(info[0]);
}
//Traverse each unsearched node
for(int i=0;i<numCourses&&valid;i++)
{
//When the node is not searched,则调用dfs
if(!visited[i])
{
dfs(i,visited,edges,valid);
}
}
return valid;
}
void dfs(int node,vector<int>&visited,vector<vector<int>>&edges,bool &valid)
{
//Set the current node as being searched
visited[node]=1;
//Recursively calls the value associated with the current node
for(int v:edges[node])
{
//0 表示存在环
if(visited[v]==0)
{
dfs(v,visited,edges,valid);
if(!valid)
{
return;
}
}
//1 show that there is a ring
else if(visited[v]==1)
{
valid=false;
return;
}
}
//2 Indicates that all nodes have been completed
visited[node]=2;
}
边栏推荐
- json根据条件存入数据库
- redis的详细介绍与操作命令
- All volunteers V853 chip Tina RTSP environment set up
- 【愚公系列】华为云云数据库MySQL的体验流程|【华为云至简致远】
- Install Update(Patches) on ESXi
- 【软件工程之美 - 专栏笔记】40 | 最佳实践:小团队如何应用软件工程?
- First online!Messaging middleware fairy notes, covering the essence of Alibaba's ten years of technology
- 用于视觉语言导航的自监督三维语义表示学习
- A16z:为什么 NFT 创作者要选择 cc0?
- 小实验:实现一个用于计算(包括加减乘除)的小程序
猜你喜欢
抓住时代趋势,网赚新逻辑:平台+个人模式超清晰解读(附产品评测)
手把手教你uniapp接入聊天IM即时通讯功能-源码分享
10分钟快速入门RDS【华为云至简致远】
Teach you how to use uniapp to access chat and IM instant messaging - source code sharing
web-sql注入
Take you to play with the "Super Cup" ECS features and experiment on the pit [HUAWEI CLOUD is simple and far]
如何制作网页
LED显示屏在会议室如何应用
UTF-8 BOM文件导致配置文件无法读取
带你玩转“超大杯”ECS特性及实验踩坑【华为云至简致远】
随机推荐
在 Fedora 上使用 SSH 端口转发
鹏城杯部分WP
web automation headless mode
Superset 1.2.0 安装
jupyter notebook 隐藏&显示全部输出内容
groovy基础学习
大佬们,sqlserver-cdc任务报错这个,大家遇到过吗Caused by: org.apac
分享这些2022设计师们决不能错过的Blender新插件
10分钟快速入门RDS【华为云至简致远】
codeforces 444C DZY Loves Colors
Flutter的实现原理初探
leetcode/删除链表的倒数第n个结点
腾讯云产品可观测最佳实践 (Function)
用于视觉语言导航的自监督三维语义表示学习
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision
[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
Teach you how to use uniapp to access chat and IM instant messaging - source code sharing
瑞吉外卖学习笔记3
科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来
pytorch安装过程中出现torch.cuda.isavailable()=False问题