[leetcode] 207.Course Schedule
2015-07-20 10:45
225 查看
题目:
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.
题意:
有n门课程,编号是0到n-1。有些课程需要依赖其他先修课程修完才能再去修读,比如对于一个pair[0,1]代表就是要修读课程1必须先修读课程0。
判断n门课程能否都修读完,比如[0,1],[1,0],意思1号课程需要依赖0号课程,并且0号课程需要依赖1号。
思路:
这是一道关于拓扑排序的问题,我们可以使用深度优先或者广度优先遍历方法。先将不需要先修课程的课程号装入,然后将其出队列,并且将所有依赖这门课的课程的依赖数减去1,如果减之后变成了0,那么将这门课程加入队列。因为此时代表该课程的先修科目已经全部修完。
以上。
代码如下:
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.
题意:
有n门课程,编号是0到n-1。有些课程需要依赖其他先修课程修完才能再去修读,比如对于一个pair[0,1]代表就是要修读课程1必须先修读课程0。
判断n门课程能否都修读完,比如[0,1],[1,0],意思1号课程需要依赖0号课程,并且0号课程需要依赖1号。
思路:
这是一道关于拓扑排序的问题,我们可以使用深度优先或者广度优先遍历方法。先将不需要先修课程的课程号装入,然后将其出队列,并且将所有依赖这门课的课程的依赖数减去1,如果减之后变成了0,那么将这门课程加入队列。因为此时代表该课程的先修科目已经全部修完。
以上。
代码如下:
class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { int size = prerequisites.size(); if(size == 0)return true; vector<int> degree(numCourses, 0); unordered_multimap<int,int> preMap; for(auto ele:prerequisites) { degree[ele.second]++; preMap.insert(ele); } int count = 0; queue<int> que; for(int i = 0; i < numCourses; i++) { if(degree[i] == 0) que.push(i); } while(!que.empty()) { count++; int pre = que.front(); que.pop(); auto res = preMap.equal_range(pre); for(auto beg = res.first; beg != res.second; beg++) { degree[beg->second]--; if(degree[beg->second] == 0) que.push(beg->second); } } if(count!= numCourses)return false; else return true; } };
相关文章推荐
- Matlab用过函数备忘记录
- iOS开发点滴-添加阴影效果
- 行人检测(Pedestrian Detection)资源
- EasyUI——DataGrid中嵌入Radio
- iOS开发点滴-添加阴影效果
- ThinkPHP的cookide保存二维数组的方法
- OpenStack Weekly Rank 2015.07.20
- 2015GitWebRTC编译实录3
- 正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长度
- spark1.4配置安装
- dump文件生成---备忘
- IntelliJ IDEA 11.0.1安装python为例
- JMX学习笔记(二)-Notification
- Calculator源码分析
- 基础知识
- Android改变状态栏颜色导航不变
- Java - Java Heap Space or Java Heap Memory
- flask-restful 快速入门
- 大数据性能调优之HBase的RowKey设计
- MatLab~Help Document