【刷题-LeetCode】207. Course Schedule
- Course Schedule
There are a total of numCourses
courses you have to take, labeled from 0
to numCourses-1
.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.
Constraints:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
1 <= numCourses <= 10^5
解法1 拓扑排序。如果能完整排序,说明可以修完所有的课程
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>>g(numCourses);
vector<int>d(numCourses, 0);
build_graph(numCourses, prerequisites, g, d);
return topological_sort(numCourses, g, d);
}
void build_graph(int numCourses, vector<vector<int>>& prerequisites,
vector<vector<int>>&g, vector<int>&d){
for(auto x: prerequisites){
g[x[1]].push_back(x[0]);
d[x[0]]++;
}
}
bool topological_sort(int numCourses, vector<vector<int>> &g, vector<int>&d){
vector<bool>vis(numCourses, false);
bool flag;
while(1){
int u = -1;
flag = false;
for(int i = 0; i < numCourses; ++i){
if(d[i] == 0 && vis[i] == false)u = i;
if(d[i] != 0)flag = true;
}
if(u == -1)break;
vis[u] = true;
for(auto v : g[u])d[v]--;
}
return !flag;
}
};
解法2 dfs。枚举每个节点,看该节点能不能形成环
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>>g(numCourses);
vector<int>d(numCourses, 0);
build_graph(numCourses, prerequisites, g, d);
vector<bool>tested(numCourses, false); // 避免对同一条路径上的节点重复测试
for(int s = 0; s < numCourses; ++s){
if(!tested[s]){
vector<bool>vis(numCourses, false);
bool flag = false;
dfs(g, s, vis, tested, flag);
if(flag)return false;
}
}
return true;
}
void dfs(vector<vector<int>> &g, int s, vector<bool>&vis, vector<bool> &tested, bool &flag){
if(flag)return;
for(auto v: g[s]){
if(vis[v]){
flag = true;
return;
}
vis[v] = true;
dfs(g, v, vis, tested, flag);
vis[v] = false;
}
tested[s] = true;
}
void build_graph(int numCourses, vector<vector<int>>& prerequisites,
vector<vector<int>>&g, vector<int>&d){
for(auto x: prerequisites){
g[x[1]].push_back(x[0]);
d[x[0]]++;
}
}
};
【刷题-LeetCode】207. Course Schedule的更多相关文章
- LeetCode刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
- LeetCode - 207. Course Schedule
207. Course Schedule Problem's Link ---------------------------------------------------------------- ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- [LeetCode] 207. Course Schedule 课程安排
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- 【刷题-LeetCode】210. Course Schedule II
Course Schedule II There are a total of n courses you have to take, labeled from 0 to n-1. Some cour ...
- [LeetCode] 207. Course Schedule 课程清单
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- [刷题] Leetcode算法 (2020-2-27)
1.最后一个单词的长度(很简单) 题目: 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度. 如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词. 如果不存在 ...
- (medium)LeetCode 207.Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- .Net Core 项目发布在IIS上 访问404 问题对应
对策: 1.进入线程池画面,将当前程序的线程池设为"无托管代码" 2.修改配置文件 Web.config,加上配置 原因: 因为.NetCore 5.0 自带集成了Swag ...
- 使用xlsx实现Excel导入
需求 实现在系统里批量导入数据,通过上传一个excel文件,前端将文件处理为json数据发送给后端.(最好与后端定义好上传的文件模板,方便处理数据) 实现 使用xlsx: xlsx的github地址: ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】525. Contiguous Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- 如何改善win10录屏时声音降噪(消除杂音)
此文章是针对win10系统中安装Realtek声卡的麦克风出现杂音的设置办法 1. 打开win10的控制面板,找到"硬件和声音选项" 2. 进入"硬件和声音"选 ...
- Java中PO、DO、DTO、 VO、 BO、POJO 、DAO、TO的概念
1. PO(persistant object) 持久对象 在 O/R 映射的时候出现的概念,如果没有 O/R 映射,没有这个概念存在了. 通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的 ...
- CoGAN
目录 概 主要内容 代码 Liu M., Tuzel O. Coupled Generative Adversarial Networks. NIPS, 2016. 概 用GAN和数据(从边缘分布中采 ...
- 本地修改配置hosts文件解决Github加载慢问题
本地修改配置hosts文件解决Github加载慢问题 手动方式 hosts 文件在每个系统的位置不一,详情如下: Windows 系统:C:\Windows\System32\drivers\etc\ ...
- <学习opencv>跨平台和本机windows
/*=========================================================================*/ // 跨平台和本机Windows /*=== ...
- win下如何优雅的使用 Burp Suite最新版
众所周知国内我们使用的 Burp Suite 大多数是大佬们分享出来的专-业-破-jie-版的 Burp Suite,每次启动的时候都得通过加载器来启动 Burp Suite,那有没有更加优雅的方式呢 ...