LeetCode207. Course Schedule
Description
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:
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.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.
my program
思路:拓扑排序
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> tmp(numCourses, 0);
multimap<int, int> mp;
for (int i = 0; i < prerequisites.size(); i++) {
tmp[prerequisites[i].second]++;
mp.insert(make_pair(prerequisites[i].first, prerequisites[i].second));
}
while (1) {
auto it = find(tmp.begin(), tmp.end(), 0);
if (it == tmp.end())
break;
int index = it - tmp.begin();
tmp[index] = -1;
auto beg = mp.lower_bound(index);
auto end = mp.upper_bound(index);
for (;beg != end; beg++) {
tmp[beg->second]--;
}
}
for (auto i : tmp) {
if (i != -1) {
return false;
}
}
return true;
}
};
Submission Details
37 / 37 test cases passed.
Status: Accepted
Runtime: 33 ms
other program
有向无环图的判断可采用dfs或bfs,至于生成图的形式可以是邻接矩阵,也可以是邻接表。为了减小时间复杂度,以下采用邻接表的方法。
以空间换时间
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int>> mp(numCourses, vector<int>());
for (int i = 0; i < prerequisites.size(); i++) {
mp[prerequisites[i].first].push_back(prerequisites[i].second);
}
vector<bool> isVisited(numCourses, false);
for (int i = 0; i < numCourses; i++) {
if (!isVisited[i]) {
vector<bool> onStack(numCourses, false);
if (hasCycle(mp, i, isVisited, onStack))
return false;
}
}
return true;
}
bool hasCycle(vector<vector<int>>& mp, int i, vector<bool>& isVisited, vector<bool>& onStack) {
isVisited[i] = true;
onStack[i] = true;
for (auto k : mp[i]) {
if (onStack[k])
return true;
else if (hasCycle(mp, k, isVisited, onStack))
return true;
}
onStack[i] = false;
return false;
}
};
Submission Details
37 / 37 test cases passed.
Status: Accepted
Runtime: 22 ms
LeetCode207. Course Schedule的更多相关文章
- Leetcode207. Course Schedule课程表
现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量以及它 ...
- [Swift]LeetCode207. 课程表 | 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] 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 ...
- [LeetCode] Course Schedule 课程清单
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- POJ 1325 Machine Schedule——S.B.S.
Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13731 Accepted: 5873 ...
- Spring Schedule 任务调度实现
我们都知道任务调度可以用Quartz,但对于简单的定时任务,可以直接用Spring内置的Schedule来实现.可以由两种方式,注释+XML配置 注解方式: 注解也要先在sping.xml配置文件中配 ...
- HDU 3572 Task Schedule(拆点+最大流dinic)
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)
在spring 中的新引入的task 命名空间.可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式. 第一步: 在Spring的相关配置文件中(applicationContex ...
- Schedule 学习
现在做的项目都有用到Schedule,现在用一点时间来总结. 一.首先要到Nuget中下载Quartz.net. 二.下载下来了,你需要对它进行配置,使它能给你正常的使用. 三.在Global.asa ...
随机推荐
- URL Schemes(转载)
URL Schemes 应用在 iOS 上已经很久了.对于使用者来说,在沙盒机制下的 iOS 中,如果想做到一定程度上的自动化就不可避免地要用到 URL Schemes.但因为 URL Schemes ...
- pytimechart使用
参考网站:http://pythonhosted.org/pytimechart/userguide.html 安装: sudo apt-get install python-chaco python ...
- 连接zookeeper集群
连接到ZooKeeper集合 ZooKeeper类通过其构造函数提供connect功能.构造函数的签名如下 : ZooKeeper(String connectionString, int sessi ...
- github清理,记录一些有趣的项目
1. rhino 一种java做的开源javascript引擎 https://github.com/mozilla/rhino 2. jeewx 国人写的公众号管理后台,集成度有些高,不好剥离.还是 ...
- [shell编程] sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory
转载地址:http://www.cnblogs.com/pipelone/archive/2009/04/17/1437879.html 在Linux中执行.sh脚本,异常/bin/sh^M: bad ...
- osgMulitiplerendertargets sample 中fbo使用【HTC VIVE开发中应用】
osgmultiplerendertargets.cpp ...................................... // now create the camera to do t ...
- chrome护眼模式
chrome护眼模式 使用stylish插件: 学习:https://jingyan.baidu.com/article/b907e627f74df146e6891c67.html 插件下载:http ...
- Required MultipartFile parameter 'file' is not present error
<input type=“file”> 中的name 与id 属性 与 addbanner(@RequestParam("file") MultipartFile ...
- Unity3D 图形问题之怎样使用水?
怎样使用水? 注意:本页所述内容仅仅适用于台式机编辑器模式. Unity 的标准资源和专业版标准资源包 (Standard Assets and Pro Standard Assets pack ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何让不同的PLC程序分线程运行 TC3
右击Tasks,添加一个新的Task 可以为这个线程设置自定义的扫描周期 然后在项目上右击添加Referenced Task 在TaskSub1上右击添加现有项,把之气写好的PRG程序绑定 ...