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的更多相关文章

  1. Leetcode207. Course Schedule课程表

    现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量以及它 ...

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

  3. [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 ...

  4. [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 ...

  5. POJ 1325 Machine Schedule——S.B.S.

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13731   Accepted: 5873 ...

  6. Spring Schedule 任务调度实现

    我们都知道任务调度可以用Quartz,但对于简单的定时任务,可以直接用Spring内置的Schedule来实现.可以由两种方式,注释+XML配置 注解方式: 注解也要先在sping.xml配置文件中配 ...

  7. HDU 3572 Task Schedule(拆点+最大流dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)

    在spring 中的新引入的task 命名空间.可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式. 第一步: 在Spring的相关配置文件中(applicationContex ...

  9. Schedule 学习

    现在做的项目都有用到Schedule,现在用一点时间来总结. 一.首先要到Nuget中下载Quartz.net. 二.下载下来了,你需要对它进行配置,使它能给你正常的使用. 三.在Global.asa ...

随机推荐

  1. fidder模拟post提交到PHP遇到的问题

    http头必须带上Content-type: application/x-www-form-urlencoded  之后 ,php 才能接收到post数据 1. 用php://input可以很便捷的取 ...

  2. Redis Exception: Exceeded timeout of 00:00:03

    Redis Exception: Exceeded timeout of 00:00:03 居然是 重启了网管, 把网络禁用重启就好了. 服 最终更新: 原来是架构湿      设置为每分钟只能读取6 ...

  3. python获取系统信息,

    1 操作系统系统综合信息cur_sysinfo=platform.uname()浏览器信息cur_browserinfo=self.driver.capabilities['version']浏览器新 ...

  4. http://blog.csdn.net/fbysss/article/details/8024748

    http://blog.csdn.net/fbysss/article/details/8024748

  5. mysql 5.7 安装手册(for linux)

    1.下载和解压mysql数据库   wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-x86_6 ...

  6. Druid对比Hadoop

    Druid对比Hadoop Hadoop 向世界证明, 花费很少的钱实现典型的解决方案, 将数据保存在一般的商用机器的数据仓库里是可行的. 当人们将自己的数据保存在Hadoop, 他们发现两个问题   ...

  7. 解决spring boot启动报错java.lang.NoClassDefFoundError: ch/qos/logback/classic/Level

    解决spring boot启动报错java.lang.NoClassDefFoundError: ch/qos/logback/classic/Level 学习了:https://blog.csdn. ...

  8. autoconfig.xml与antx.properties一级application.properties之间的关系

    Java web项目中一般都有配置文件,文件中包含一些配置信息供Java工程启动和运行时使用,这些常见的配置文件大都是一些以.properties后缀的文件,比如常见的antx.properties以 ...

  9. css3的nth-child选择器的具体探讨

    css3的nth-child选择器的具体探讨 前言 在十年前開始的div+css布局兴起之时,我就開始了CSS的学习和实践.在当年,对于CSS选择器,基本上是没有什么选择性的,仅仅有ID选择器,CLA ...

  10. HTTP——代理协议 HTTP/1.1的CONNECT方法

    我们平时使用HTTP协议无非就是GET.POST这些方法,但是HTTP的内容远不止那些.今天就来说说HTTP代理使用的CONNECT.这个不是在网页开发上用的,如果没兴趣就跳过吧. APACHE只是作 ...