Course Schedule
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.
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
- Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
- Topological sort could also be done via BFS.
本题最终转化为求有向图中是否有存在环,转化为拓扑序问题
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int>> graph(numCourses);
vector<int> in_degree(numCourses, );
for (auto p : prerequisites) {
graph[p.second].push_back(p.first);
in_degree[p.first]++;
}
queue<int> q;
int cnt = ;
for (int i = ; i < numCourses; i++) {
if (in_degree[i] == )
q.push(i);
}
while (!q.empty()) {
int cur = q.front();
q.pop();
for (auto it = graph[cur].begin(); it != graph[cur].end(); it++) {
if (--in_degree[*it] == )
q.push(*it);
}
}
for (int i = ; i < numCourses; i++) {
if (in_degree[i] != )
return false;
}
return true;
}
};
Course Schedule的更多相关文章
- [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 ...
- Spring 4.x Task 和 Schedule 概述(代java配置)
转载请注明https://zhangzhaoyu.github.io/2016/09/30/spring-task-and-schedule-deep-research/ 摘要 在很多业务场景中,系统 ...
- OpenMP并行构造的schedule子句详解 (转载)
原文:http://blog.csdn.net/gengshenghong/article/details/7000979 schedule的语法为: schedule(kind, [chunk_si ...
- [nRF51822] 4、 图解nRF51 SDK中的Schedule handling library 和Timer library
:nRF51822虽然是一个小型的单片机,但是能真正达到任意调用其官方驱动以及BLE协议栈的人还是奇缺的.据我所见,大都拿官方给的一个冗长的蓝牙低功耗心率计工程改的.之前我对于这个工程进行log跟踪, ...
随机推荐
- CentOS x64上Matlab R2015b的镜像安装方法与卸载
0. 原料 (1). CentOS_x64系统 CentOS 2.6.32-573.el6.x86_64 (2). Matlab R2015b_glnxa64.iso,可以从百度网盘下载到:链接: ...
- mysql数据库使用
C#操作Mysql数据库的存储过程,网址 DATEDIFF() 函数返回两个日期之间的天数. 语法 DATEDIFF(date1,date2) date1 和 date2 参数是合法的日期或日期/时间 ...
- Python Day03
set Collections系列: Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几 ...
- Swift 2.0 异常处理
转自:http://www.jianshu.com/p/96a7db3fde00 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主 ...
- i7 4790 z97-ar ssd 固态硬盘 装机的一些经历
今天电脑终于装好了,我于七夕情人节前后的几天配了一台台式机用开发,期间遇到了一些问题与大家分享一下,希望对一些朋友有帮助. 1 买HIMD线,各个零件全部组装好后,我意识到一个问题没买HIMD线,我的 ...
- eclipse + python dev
错误:Project interpreter not specified解决方法 http://blog.csdn.net/magictong/article/details/7288732 安装Py ...
- python2.7安装matplotlib遇到的问题及解决方法
python2.7下import matplotlib报错 第一个报错是:缺少这个pyparsing-2.1.4.win32-py2.7.exe 直接下载安装就行 第二个报错是:缺少cycler 这个 ...
- Comparison method violates its general contract 关于jdk自带算法问题
昨晚上线,线上报了一个问题,用的jdk8,用的collections.sort方法, public static void main(String[] args) { List<Integer& ...
- 2.4G/5G频段WLAN各国使用信道表
List of WLAN channels (维基百科):https://en.wikipedia.org/wiki/List_of_WLAN_channels 2.4G 5G 另附美国5G允许使用的 ...
- TypeScript Handbook 2——接口1(翻译)
接口(Interfaces) One of TypeScript's core principles is that type-checking focuses on the 'shape' that ...