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 ...
随机推荐
- Entity Framework part1
First Demo实体框架Entity Framework,简称EFEF是微软推出的基于Ado.Net的数据库访问技术,是一套ORM框架底层访问数据库的实质依然是ado.net是一套orm框架,即框 ...
- sql字段字符用做其他类型查询
select * FROM aa where parent = %@ ORDER BY cast(seq as integer) ASC
- 使用.Net中的WeakDictionary — ConditionalWeakTable
有的时候,我们需要给某些数据添加一些附加信息,一种常用的做法是使用一个Dictionary在填充这些附加信息如: var data = new Data(); var tag = new Tag ...
- 【mybatis】mybatis中insert 主键自增和不自增的插入情况【mysql】
主键不自增:返回值是插入的条数 <insert id="add" parameterType="EStudent"> insert into TSt ...
- easyui-validatebox 的简单长度验证
验证: 页面代码: <form id="invoiceEdit"> <input id="fpdm" name="fpdm" ...
- Go语言的9大优势和3大缺点, GO语言最初的定位就是互联网时代的C语言, 我为什么放弃Go语言
Go语言的9大优势和3大缺点 转用一门新语言通常是一项大决策,尤其是当你的团队成员中只有一个使用过它时.今年 Stream 团队的主要编程语言从 Python 转向了 Go.本文解释了其背后的九大原因 ...
- [GitHub开源]基于HTML5实现的轻量级Google Earth三维地图引擎,带你畅游世界 【转】
http://blog.csdn.net/iispring/article/details/52679185 WebGlobe HTML5基于原生WebGL实现的轻量级Google Earth三维地图 ...
- Appstore 提交时错误
ERROR ITMS-9000:This boundle is invalid. New apps and app updates submitted to the App Store must ...
- 《Java程序猿面试笔试宝典》之Statickeyword有哪些作用
statickeyword主要有两种作用:第一,仅仅想为某特定数据类型或对象分配单一的存储空间,而与创建对象的个数无关.第二,希望某个方法或属性与类而不是对象关联在一起,也就是说,在不创建对象的情况下 ...
- 对非正确使用浮点型数据而导致项目BUG的问题探讨
乘法分配律 在上小学的时候就已经学习过乘法分配律,乘法分配律的详细内容是:两个数的和与一个数相乘.能够先把他们分别与这个数相乘,再相加.得数不变.乘法分配律的定义还能够用表达式"(a+b)× ...