[LeetCode] 207. Course Schedule 课程表
题目:

分析:
这是一道典型的拓扑排序问题。那么何为拓扑排序?
拓扑排序:
有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A>C>B。
总的来说,拓扑排序就是对于相互之间有先后依赖关系的事件,给出一个行之有效的序列。
图解释:
反应到图中,就是一个有向无环图可以被拓扑排序,而有环图是无法进行拓扑排序的(比如,A依赖B,B依赖C,C依赖A,三者形成了环,是没有办法完成的)
很明显“课程表”这个题目就是判断题目所给的依赖关系,是否可以进行拓扑排序,或是否是有向无环图
思路:
对于拓扑排序,一般有两种方法进行判断:
1、深度优先搜索(DFS),判断是否有环
2、节点的入度,一个节点的入度是指向指向该节点的节点个数,比如拓扑排序举的例子,A的入度为0,BC的入度均为1
这里利用节点的入度来判断是否可以进行拓扑排序,或者是否是有向无环图
只要某个节点的入度为0,就说明其没有依赖条件,可以直接输出,同时减少以该节点为起点的节点的入度
回到题目
题目已知,n个课程会被从0至n-1编号
我们需要知道每个课程的入度,可以定义一个preNum数组, preNum[i]:代表i号课程所依赖的课程数
同时在寻找0入度的课程时,我们需要知道某个课程是否已上完,可以定义一个visited数组,visited[i]:表示i号课程已上完
为了寻找0入度课程方便,构造一个getNext()函数,用来寻找入度为0的课程
代码:
class Solution {
public:
int getNext(vector<int> preNum, vector<int> visited){
for(int i=; i<preNum.size(); i++) if(!visited[i] && !preNum[i]) return i;
return -;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {int n = prerequisites.size();
vector<int> visited(numCourses);
vector<int> preNum(numCourses);
for(int i=; i<n; i++) preNum[prerequisites[i][]]++;
for(int i=getNext(preNum, visited); i!=-; i=getNext(preNum, visited)){
visited[i] = ;
numCourses--;
for(int j=; j<n; j++){
if(prerequisites[j][]==i) preNum[prerequisites[j][]]--;
}
if(!numCourses) return true;
}
return false;
}
};
当然,这段程序还有改进的余地,比如说获取下一个0入度的课程的方式,每次都利用了循环
可以动态维护一个栈,每当某个课程的入度为零时即压入栈,以此来节省时间,有兴趣的同学可以自己写一下
我做了一下测试,运行时间没有明显减少,反而是内存消耗降低了,应该是调用函数的次数减少,减少了内存的使用
[LeetCode] 207. Course Schedule 课程表的更多相关文章
- [leetcode]207. Course Schedule课程表
在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...
- 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] 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 ...
- (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 ...
- 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 p ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- smooth curve|population|sample
Distribution Shapes 由直方图到 smooth curve 1.this distribution of heights is bell shaped(or mound shap ...
- android 9.0 http无法访问问题
在res/xml下新建network-security-config.xml <?xml version="1.0" encoding="utf-8"?& ...
- python关于文件操作
今日所得 文件操作模式的补充 文件光标的移动控制 截断文件 修改文件 函数的简单介绍 文件操作模式的补充 """ r w a 将上面的三个模式称为纯净模式 r+ w+ a ...
- TPO1-3 Timberline Vegetation on Mountains
Wind velocity also increase with altitude and may cause serious stress for trees,as is made evident ...
- 点分治——POJ 1741
写的第一道点分治的题目,权当认识点分治了. 点分治,就是对每条过某个点的路径进行考虑,若路径不经过此点,则可以对其子树进行考虑. 具体可以看menci的blog:点分治 来看一道例题:POJ 1741 ...
- [LC] 318. Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- [LC] 165. Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 &l ...
- warning: LF will be replaced by CRLF in ** 的原因及解决办法
https://blog.csdn.net/man_zuo/article/details/88651416
- 浏览器证书问题,chorm,ie,edge,safari都会去读系统证书,firefox例外
坑爹 没想过浏览器兼容的问题. 为系统安装用户证书后, firefox一直无法连接 提示 连接 www.httpsserver.com:8985 时发生错误. SSL 对等端无法协商出一个可接受的安全 ...
- gin源码剖析
介绍 Gin 是一个 Golang 写的 web 框架,具有高性能的优点,基于 httprouter,它提供了类似martini但更好性能(路由性能约快40倍)的API服务.官方地址:https:// ...