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:

  1. Input: numCourses = 2, prerequisites = [[1,0]]
  2. Output: true
  3. Explanation: There are a total of 2 courses to take.
  4. To take course 1 you should have finished course 0. So it is possible.

Example 2:

  1. Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
  2. Output: false
  3. Explanation: There are a total of 2 courses to take.
  4. To take course 1 you should have finished course 0, and to take course 0 you should
  5. 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 拓扑排序。如果能完整排序,说明可以修完所有的课程

  1. class Solution {
  2. public:
  3. bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
  4. vector<vector<int>>g(numCourses);
  5. vector<int>d(numCourses, 0);
  6. build_graph(numCourses, prerequisites, g, d);
  7. return topological_sort(numCourses, g, d);
  8. }
  9. void build_graph(int numCourses, vector<vector<int>>& prerequisites,
  10. vector<vector<int>>&g, vector<int>&d){
  11. for(auto x: prerequisites){
  12. g[x[1]].push_back(x[0]);
  13. d[x[0]]++;
  14. }
  15. }
  16. bool topological_sort(int numCourses, vector<vector<int>> &g, vector<int>&d){
  17. vector<bool>vis(numCourses, false);
  18. bool flag;
  19. while(1){
  20. int u = -1;
  21. flag = false;
  22. for(int i = 0; i < numCourses; ++i){
  23. if(d[i] == 0 && vis[i] == false)u = i;
  24. if(d[i] != 0)flag = true;
  25. }
  26. if(u == -1)break;
  27. vis[u] = true;
  28. for(auto v : g[u])d[v]--;
  29. }
  30. return !flag;
  31. }
  32. };

解法2 dfs。枚举每个节点,看该节点能不能形成环

  1. class Solution {
  2. public:
  3. bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
  4. vector<vector<int>>g(numCourses);
  5. vector<int>d(numCourses, 0);
  6. build_graph(numCourses, prerequisites, g, d);
  7. vector<bool>tested(numCourses, false); // 避免对同一条路径上的节点重复测试
  8. for(int s = 0; s < numCourses; ++s){
  9. if(!tested[s]){
  10. vector<bool>vis(numCourses, false);
  11. bool flag = false;
  12. dfs(g, s, vis, tested, flag);
  13. if(flag)return false;
  14. }
  15. }
  16. return true;
  17. }
  18. void dfs(vector<vector<int>> &g, int s, vector<bool>&vis, vector<bool> &tested, bool &flag){
  19. if(flag)return;
  20. for(auto v: g[s]){
  21. if(vis[v]){
  22. flag = true;
  23. return;
  24. }
  25. vis[v] = true;
  26. dfs(g, v, vis, tested, flag);
  27. vis[v] = false;
  28. }
  29. tested[s] = true;
  30. }
  31. void build_graph(int numCourses, vector<vector<int>>& prerequisites,
  32. vector<vector<int>>&g, vector<int>&d){
  33. for(auto x: prerequisites){
  34. g[x[1]].push_back(x[0]);
  35. d[x[0]]++;
  36. }
  37. }
  38. };

【刷题-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. 搭建 3D 智慧农场可视化,解锁绿色生态田园

    前言 何为"无人农场"?中国工程院院士罗锡文用五句话高度概括:"耕种管收生产环节全覆盖:机库田间转移作业全自动:自动避障异况停车保安全:作物生产过程实施全监控:智能决策精 ...

  2. C++ 11新特性:std::future & std::shared_future) (转载)

    上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...

  3. SecureCRT中Vim颜色

    解决方法:1.确认安装了vim-enhancedrpm -qa | grep vim-enhanced2.optins>session optionsTerminal>emulationx ...

  4. JAVA使用反射获取对象的所有属性名

    public static void main(String[] args) { Field[] fields=BaseSalary.class.getDeclaredFields(); for (i ...

  5. 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)

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

  6. 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)

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

  7. Spring Boot + MyBatis + MySQL 实现读写分离

    读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做. 读写分离有两种实现方式: 第一种是依靠中间件(比如:MyCat ...

  8. delete、truncate、drop

    DELETE DELETE属于数据库DML操作语言,只删除数据不删除表的结构,会走事务,执行时会触发trigger:每次从表中删除一行,并且同时将该行的的删除操作记录在redo和undo表空间中以便进 ...

  9. 解决ubuntu突然无法联网问题

    一.问题描述 今天使用笔记本远程办公的时候,突然电脑无法联网了,使用chrome浏览器访问网页出现如下错误 This site can't be reachedwww.baidu.com's serv ...

  10. Generative Modeling by Estimating Gradients of the Data Distribution

    目录 概 主要内容 Langevin dynamics Score Matching Denoising Score Matching Noise Conditional Score Networks ...