1. 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的更多相关文章

  1. LeetCode刷题------------------------------LeetCode使用介绍

    临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...

  2. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  3. 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 ...

  4. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

  5. [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 ...

  6. 【刷题-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 ...

  7. [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 ...

  8. [刷题] Leetcode算法 (2020-2-27)

    1.最后一个单词的长度(很简单) 题目: 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度. 如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词. 如果不存在 ...

  9. (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 ...

随机推荐

  1. CF508A Pasha and Pixels 题解

    Content 有一个 \(n\times m\) 的矩阵,一开始全部格子被染成白色. 接下来有 \(k\) 个操作,每一个操作表示把一个格子染成黑色.问第一次出现 \(2\times 2\) 的全部 ...

  2. CF984B Minesweeper 题解

    Content 有一个 \(n\times m\) 的扫雷地图,请判断这个地图是否合法,即对于所有格子,是否满足: 对应点的数字周围必须有对应数字个雷 空的地方周围没有雷 数据范围:\(1\leqsl ...

  3. CF999B Reversing Encryption 题解

    Content 给一个长度为 \(n\) 的字符串 \(s\),执行以下操作: 降序遍历 \(n\) 的所有因子(从 \(n\) 到 \(1\)). 对于每一个因子 \(i\) 翻转字符串 \(s_{ ...

  4. TFTP协议介绍-python实现tftp客户端

    1. TFTP协议介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议) 是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文件传输的协议 特点: ...

  5. 请注意JS方法,方法同名,参数个数不一样是不能区分方法的,

    请注意JS方法,方法同名,参数个数不一样是不能区分方法的, 所以要区分方法,只能利用方法名不同来区分,而不能利用参数个数与参数类型来分.

  6. JAVA比较两个版本号的大小

    /** * 比较版本号的大小 (两个版本号格式应尽量相同) * * @param v1 版本号1 * @param v2 版本号2 * @return 正数:v1大 负数:v2大 0:相等 */ pu ...

  7. sql改写优化:简单规则重组实现

    我们知道sql执行是一个复杂的过程,从sql到逻辑计划,到物理计划,规则重组,优化,执行引擎,都是很复杂的.尤其是优化一节,更是内容繁多.那么,是否我们本篇要来讨论这个问题呢?答案是否定的,我们只特定 ...

  8. UDP&串口调试助手用法(5)

    note 提供安装包 基于win10开发 已通过win10测试,windows其他平台,没有测试 日志 2021-09-18 1.修复计算校验和错误的现象 2.屏蔽不计算校验和位置的REG验证(后期更 ...

  9. Qt之QListView和QStandardItemModel用法

    note 个人理解, QListView 用于 显示数据,而数据的逻辑维护则由 QStandardItemModel 完成. QStandardItemModel 创建 if (nullptr == ...

  10. 【LeetCode】544. Output Contest Matches 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...