Course Schedule ——LeetCode
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.
题目大意:给一堆课程依赖,找出是否可以顺利修完全部课程,拓扑排序。
解法一:DFS+剪枝,DFS探测是否有环,图的表示采用矩阵。
public boolean canFinish(int n, int[][] p) {
if (p == null || p.length == 0) {
return true;
}
int row = p.length;
int[][] pre = new int[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(pre[i], -1);
}
for (int i = 0; i < row; i++) {
pre[p[i][0]][p[i][1]] = p[i][1];
}
// System.out.println(Arrays.deepToString(pre));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
if (pre[i][j] != -1) {
Deque<Integer> queue = new ArrayDeque<>();
queue.offer(pre[i][j]);
Set<Integer> circleDep = new HashSet<>();
circleDep.add(pre[i][j]);
while (!queue.isEmpty()) {
int dep = queue.poll();
if (dep >= row) {
continue;
}
for (int k = 0; k < n; k++) {
if (pre[dep][k] == -1) {
continue;
}
if (circleDep.contains(pre[dep][k])) {
return false;
}
queue.offer(pre[dep][k]);
circleDep.add(pre[dep][k]);
pre[dep][k]=-1;
}
}
}
}
}
return true;
}
解法二:BFS,参考别人的思路,也是用矩阵表示图,另外用indegree表示入度,先把入度为0的加入队列,当队列非空,逐个取出队列中的元素,indegree[i]-1==0的继续入队列,BFS遍历整个图,用count记录课程数,如果等于给定值则返回true。
public boolean canFinish(int n, int[][] p) {
if (p == null || p.length == 0) {
return true;
}
int[][] dep = new int[n][n];
int[] indegree = new int[n];
for(int i=0;i<p.length;i++){
if(dep[p[i][0]][p[i][1]]==1){
continue;
}
dep[p[i][0]][p[i][1]]=1;
indegree[p[i][1]]++;
}
Deque<Integer> queue = new ArrayDeque<>();
for(int i=0;i<n;i++){
if(indegree[i]==0){
queue.offer(i);
}
}
int count = 0;
while(!queue.isEmpty()){
count++;
int cos = queue.poll();
for(int i=0;i<n;i++){
if(dep[cos][i]!=0){
if(--indegree[i]==0){
queue.offer(i);
}
}
}
}
return count==n;
}
Course Schedule ——LeetCode的更多相关文章
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [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 ...
- Java for LeetCode 210 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 ...
- Java for LeetCode 207 Course Schedule【Medium】
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 II
原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...
- [LeetCode] Course Schedule III 课程清单之三
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
- [Leetcode Week3]Course Schedule
Course Schedule题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule/description/ Descript ...
随机推荐
- GridView 无数据时,绑定提示
private void BindData() { DataTable dt = DAO.RunSQLReturnDt(this.getsql()); int dtcount = dt.Rows.Co ...
- 表中查询重复的数据,如何通过sql语句查询?
1.最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)>1所有名字重复人的 ...
- TOM大师脚本-show space 多个版本,谢谢大牛们
示例一 该脚本需区分 对象的管理方式是 自动还是 手动, 对手动管理方式 的表显示很全面 SQL> exec show_space_old('MAN_TAB','DEV','TABLE'); F ...
- 关于UIScrollView属性和方法的总结
iOS中UIScollView的总结 在iOS开发中可以说UIScollView是所有滑动类视图的基础,包括UITableView,UIWebView,UICollectionView等等,UIScr ...
- [转]一个备份MySQL数据库的简单Shell脚本
本文翻译自 iSystemAdmin 的 <A Simple Shell Script to Backup MySQL Database> Shell脚本是我们写不同类型命令的一种脚本,这 ...
- 关于JavaScript的类的继承
其实最一开始学JS的时候就看过继承的实现.当时只是去试着理解从书上看来的代码段而已.今天又重新思考了一下,感觉这是一个思维探索演进的结果. 继承,即复用. 如果抛开继承的固有思想,让b复用a的成员,最 ...
- Centos7搭建php+mysql环境(整理篇)
终于将mysql+php环境搭建成功,将之前的整理一下,环境:centos7,本机IP:192.168.1.24,数据库用户名及密码都设为root,测试文件路径:/var/www/html 1.取消c ...
- hdu 3308
终于A了,我好想砍人,虽然这是一道基础的区间合并.但是这错误我也是醉了. 错误我表在注释里. 题目意思不多说,sha崽题目出的很简洁. #include <iostream>#includ ...
- MySql数据库3【优化2】sql语句的优化
1.SELECT语句优化 1).利用LIMIT 1取得唯一行[控制结果集的行数] 有时,当你要查询一张表是,你知道自己只需要看一行.你可能会去的一条十分独特的记录,或者只是刚好检查了任何存在的记录数, ...
- shell脚本中的标准输出重定向使用涵义
0表示标准输入 1表示标准输出 2表示标准错误输出 > 默认为标准输出重定向,与 1> 相同 2>&1 意思是把 标准错误输出 重定向到 标准输出. &>fil ...