Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 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?

For example:

  1. 2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

  1. 2, [[1,0],[0,1]]

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.

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

click to show more hints.

解法一:每个连通分量,只要出现回路,即说明冲突了。

回路检测如下:

存在a->b,又存在b->c,那么增加边a->c

如果新增边c->a,发现已有a->c,在矩阵中表现为对称位置为true,则存在回路。

注:数组比vector速度快。

  1. class Solution {
  2. public:
  3. bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
  4. bool **preG;
  5. preG = new bool*[numCourses];
  6. for(int i = ; i < numCourses; i ++)
  7. preG[i] = new bool[numCourses];
  8. for(int i = ; i < numCourses; i ++)
  9. {
  10. for(int j = ; j < numCourses; j ++)
  11. preG[i][j] = false;
  12. }
  13. for(int i = ; i < prerequisites.size(); i ++)
  14. {
  15. int a = prerequisites[i].first;
  16. int b = prerequisites[i].second;
  17. if(preG[b][a] == true)
  18. return false;
  19. else
  20. {
  21. preG[a][b] = true;
  22. for(int j = ; j < numCourses; j ++)
  23. {
  24. if(preG[j][a] == true)
  25. preG[j][b] = true;
  26. }
  27. }
  28. }
  29. return true;
  30. }
  31. };

解法二:不断删除出度为0的点,如果可以逐个删除完毕,说明可以完成拓扑序,否则说明存在回路。

  1. class Solution {
  2. public:
  3. bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
  4. vector<int> outd(numCourses, );
  5. vector<bool> del(numCourses, false);
  6. unordered_map<int, vector<int> > graph;
  7. // construct reverse neighborhood graph
  8. // graph is to decrease the out-degree of a set of vertices,
  9. // when a certain vertice is deleted
  10. for(int i = ; i < prerequisites.size(); i ++)
  11. {
  12. outd[prerequisites[i].first] ++;
  13. graph[prerequisites[i].second].push_back(prerequisites[i].first);
  14. }
  15. int count = ;
  16. while(count < numCourses)
  17. {
  18. int i;
  19. for(i = ; i < numCourses; i ++)
  20. {
  21. if(outd[i] == && del[i] == false)
  22. break;
  23. }
  24. if(i < numCourses)
  25. {
  26. del[i] = true; // delete
  27. for(int j = ; j < graph[i].size(); j ++)
  28. {// decrease the degree of vertices that links to vertice_i
  29. outd[graph[i][j]] --;
  30. }
  31. count ++;
  32. }
  33. else
  34. {// no vertice with 0-degree
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. };

【LeetCode】207. Course Schedule (2 solutions)的更多相关文章

  1. 【LeetCode】207. Course Schedule 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/course-s ...

  2. 【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 ...

  3. 【LeetCode】210. Course Schedule II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拓扑排序,BFS 拓扑排序,DFS 参考资料 日期 ...

  4. 【刷题-LeetCode】207. Course Schedule

    Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...

  5. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  6. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  7. 【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 co ...

  8. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  9. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. LSTM简介以及数学推导(FULL BPTT)

    http://blog.csdn.net/a635661820/article/details/45390671 前段时间看了一些关于LSTM方面的论文,一直准备记录一下学习过程的,因为其他事儿,一直 ...

  2. win8下Source Insight has not been installed completely问题的解决

    系统:windows8 软件:Source Insight 3.5 安装后打开总是提示如下图错误,没法使用. 卸载重新安装好多次,还是不行,百度一下,终于找到方法,记录一下,方便以后查找. 解决方法: ...

  3. 你应该知道的Linux历史

    说道linux的历史不得不说的就是unix,我们的linux就是类unix系统: 1969年第一台unix系统 贝尔实验室 1970年C语言诞生,对unix内核重新编写 system V(AT& ...

  4. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(四)针对hadoop2.9.0启动执行start-all.sh出现异常:failed to launch: nice -n 0 /bin/spark-class org.apache.spark.deploy.worker.Worker

    启动问题: 执行start-all.sh出现以下异常信息: failed to launch: nice -n 0 /bin/spark-class org.apache.spark.deploy.w ...

  5. 主成分分析(PCA)原理及推导

    原文:http://blog.csdn.net/zhongkejingwang/article/details/42264479 什么是PCA? 在数据挖掘或者图像处理等领域经常会用到主成分分析,这样 ...

  6. create-react-app时registry的奇怪问题

    用React官方给的NPM脚本 create-react-app my-app 在自动安装module的过程中,在安装registry的组件的时候莫名其妙的挂住不动了.界面显示的信息如下: fetch ...

  7. ASP入门(二)-创建Access数据库

    通常来说,ASP程序是搭配Access数据库来使用的,因此在安装完ASP环境后,为了方便建立和管理数据库,我们还需要安装Access数据库. Access是Microsoft Office家族中的一员 ...

  8. 放入MP3的文件夹显示一些没用的标题,艺术家,唱片集怎么办?

    原来的文件大小,类型,修改日期,创建日线都没了,怎么办? 右键点击名称,标题,艺术家那一行,在右键菜单中,去掉没用的,勾选有用的,如下图:

  9. LabVIEW上位机与串口通信

    渊源 大一的时候,学校开了门公共选修课,叫LabVIEW编程,当时的我当然还不知道LabVIEW是啥东东,但还是选了.上课的老师是机械学院的一个副教授.他给我们展示了好几个用LabVIEW做的项目.譬 ...

  10. springboot项目启动多个实例的方法

    我现在需要实现这样的功能:将一个服务提供者启动多个实例,下面我列出在eclipse中启动多个实例的方法: 首先看一下我的服务提供者的项目文件结构: springboot默认的配置文件是applicat ...