【刷题-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 ...
随机推荐
- redis启动报错:Fatal error loading the DB: Invalid argument
redis启动报错 add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be ...
- JAVA生成订单编号工具类
JAVA根据时间戳和随机数方式生成订单编号工具类 OrderUtils.java package com.util; import java.text.DateFormat; import java. ...
- SpringBoot 整合Spring Security框架
引入maven依赖 <!-- 放入spring security依赖 --> <dependency> <groupId>org.springframework.b ...
- c++基础之operator =处理
1.注意自我赋值 先看个例子: class A {}; A a ; a = a; // 注意这句 可能实际中,你不会这样做,但是实际中,是有可能的,并且这样做,也不违背语法. BUT, 如果上面的例子 ...
- 细聊.NET6 ConfigurationManager的实现
前言 友情提示:建议阅读本文之前先了解下.Net Core配置体系相关,也可以参考本人之前的文章<.Net Core Configuration源码探究>然后对.Net Core的Conf ...
- 【LeetCode】1466. 重新规划路线 Reorder Routes to Make All Paths Lead to the City Zero (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://lee ...
- 【九度OJ】题目1069:查找学生信息 解题报告
[九度OJ]题目1069:查找学生信息 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1069 题目描述: 输入 ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- Grids
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- Contrastive Generative Adversarial Networks
目录 概 主要内容 代码 Kang M., Park J. Contrastive Generative Adversarial Networks. arXiv preprint arXiv 2006 ...