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. SecureCRT的一些问题解决

    按下退格键发送删除命令 设置缓冲 拷贝与粘贴 多标签切换   ctrl + tab . 如果同时按下shift,可以方向切换

  2. Java笔记7:最简单的网络请求Demo

    一.服务器端 1 新建一个工程,建立一个名为MyRequest的工程. 2 FileàProject StructureàModulesà点击最右侧的“+”àLibraryàJava 找到Tomcat ...

  3. 转:ios学习指南

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Franz Fang链接:https://www.zhihu.com/question/20264108/answer/302 ...

  4. C# /windowForm/WPF/SilverLight里面操作Word帮助类提供给大家

    很多的程序都需要用到对word的操作,数据库里面的表需要一书面的形式展示出来,最近在的一个项 using System; using System.Collections.Generic; using ...

  5. perl学习笔记——正则表达式

    正则表达式 简单模式:匹配$_中的内容,只需要将模式写在一对斜线(/)中就可以了. 如:#!/usr/bin/env perl use 5.010; $_="yabba dabba doo& ...

  6. C#秘密武器之LINQ to SQL

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  7. 一张图片教会你写mysql 语句

    MySQL的语句执行顺序 MySQL的语句一共分为11步,如下图所标注的那样,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入 ...

  8. Apache 使用gzip、deflate 压缩页面加快网站访问速度

    Apache 使用gzip 压缩页面加快网站访问速度 介绍: 网页压缩来进一步提升网页的浏览速度,它完全不需要任何的成本,只不过是会让您的服务器CPU占用率稍微提升一两个百分点而已或者更少.   原理 ...

  9. Linux /bin, /sbin, /usr/bin, /usr/sbin 区别(转)

    在linux下我们经常用到的四个应用程序的目录是:/bin./sbin./usr/bin./usr/sbin    bin:  bin为binary的简写主要放置一些系统的必备执行档例如:cat.cp ...

  10. [翻译]JUnit 5 用户手册

    为了系统的学习下 JUnit 5, 因此开始翻译 JUnit 5 官方用户手册, 谢谢关注! 本手册翻译自 5.0.0-M4 版本. 若感兴趣, 可 star 或 fork 该仓库! GitHub: ...