Course Schedule I & II
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.
From: http://www.programcreek.com/2014/05/leetcode-course-schedule-java/
分析:
建立有向图,利用Toplogical sort逐一去掉没有father的节点。如果最后总的节点大于没有父节点的个数,表面里面有环。
public boolean canFinish(int numCourses, int[][] prerequisites) {
if (prerequisites == null) return true;
Map<Integer, Node> map = new HashMap<Integer, Node>();
for (int[] row : prerequisites) {
if (!map.containsKey(row[])) {
map.put(row[], new Node(row[], ));
}
if (!map.containsKey(row[])) {
map.put(row[], new Node(row[], ));
}
map.get(row[]).inDegree++;
map.get(row[]).list.add(map.get(row[]));
}
Queue<Node> queue = new LinkedList<Node>();
for (Node node : map.values()) {
if (node.inDegree == ) {
queue.offer(node);
}
}
while (!queue.isEmpty()) {
Node node = queue.poll();
for (Node child : node.list) {
child.inDegree--;
if (child.inDegree == ) {
queue.offer(child);
}
}
}
for (Node node : map.values()) {
if (node.inDegree != ) {
return false;
}
}
return true;
}
}
class Node {
int value;
int inDegree;
List<Node> list;
public Node(int value, int inDegree) {
this.value = value;
this.inDegree = inDegree;
list = new ArrayList<Node>();
}
}
还有一种方法就是用dfs来看是否有back edge.
class Solution {
public static boolean hasCycle(List<List<Integer>> graph) {
// null input checks, etc
int numNodes = graph.size();
boolean[] visited = new boolean[numNodes];
boolean[] current = new boolean[numNodes];
for (int i = ; i < numNodes; ++i) {
if (!visited[i]) {
boolean foundCycle = dfs(graph, visited, current, i);
if (foundCycle) {
return true;
}
}
}
return false;
}
private static boolean dfs(List<List<Integer>> graph, boolean[] visited, boolean[] current, int pos) {
if (current[pos]) {
return true;
}
if (visited[pos]) {
return false;
}
visited[pos] = true;
current[pos] = true;
List<Integer> adj = graph.get(pos);
for (int dest : adj) {
boolean res = dfs(graph, visited, current, dest);
if (res) {
return true;
}
}
current[pos] = false;
return false;
}
}
Course Schedule II
找出选课的顺序。
public class Solution {
public int[] findOrder(int n, int[][] prerequisites) {
if (prerequisites == null) return new int[];
Map<Integer, Node> map = new HashMap<Integer, Node>();
// very important
for (int i = ; i < n; i++){
map.put(i, new Node(i, ));
}
for (int[] row : prerequisites) {
map.get(row[]).inDegree++;
map.get(row[]).list.add(map.get(row[]));
}
List<Integer> list = new ArrayList<Integer>();
Queue<Node> queue = new LinkedList<Node>();
for (Node node : map.values()) {
if (node.inDegree == ) {
queue.offer(node);
}
}
while (!queue.isEmpty()) {
Node node = queue.poll();
list.add(node.value);
for (Node child : node.list) {
child.inDegree--;
if (child.inDegree == ) {
queue.offer(child);
}
}
}
if (list.size() < n) return new int[];
int[] result = new int[list.size()];
for (int i = ; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
}
class Node {
int value;
int inDegree;
List<Node> list;
public Node(int value, int inDegree) {
this.value = value;
this.inDegree = inDegree;
list = new ArrayList<Node>();
}
}
Course Schedule I & II的更多相关文章
- [LeetCode] Course Schedule I (207) & II (210) 解题思路
207. Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some ...
- 【LeetCode】210. Course Schedule II
Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
- 【刷题-LeetCode】210. Course Schedule II
Course Schedule II There are a total of n courses you have to take, labeled from 0 to n-1. Some cour ...
- [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 ...
- 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 ...
- LeetCode Course Schedule II
原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...
- FB面经prepare: task schedule II
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...
- Leetcode 210 Course Schedule II
here are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prere ...
随机推荐
- linux 关机命令总结
linux下常用的关机命令有:shutdown.halt.poweroff.init:重启命令有:reboot.下面本文就主要介绍一些常用的关机命令以及各种关机命令之间的区别和具体用法. 首先来看一下 ...
- 关于php cgi的配置
http://blog.csdn.net/xiaolei1982/article/details/7103850 1,查看php-cgi的进程数 netstat -anpo | grep " ...
- [译]git clone
git clone git clone命令copy一个已经存在的Git仓储. git clone有点像svn的checkout, 他的不同之处是这个copy也是一个完整的仓储-它有自己的历史纪录, 能 ...
- solr多条件查询(二)
由于现在的 需求很变态需要N多条件的叠加,本人就用了一天时间摸索加求助,终于参透出这个q和fq的强大之处. 需求如下图,有三种关系:并且.或.不含 1.如果是或者也就是改变的q的查询条件: 2.如果是 ...
- resin
http://blog.csdn.net/sea0x/article/details/6097531 resin 启动: resin 配置文件摘取: <server-default> &l ...
- Java并发包源码学习之AQS框架(三)LockSupport和interrupt
接着上一篇文章今天我们来介绍下LockSupport和Java中线程的中断(interrupt). 其实除了LockSupport,Java之初就有Object对象的wait和notify方法可以实现 ...
- myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入
com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...
- maven之window安装
1.下载:apache-maven-3.3.9-bin.zip 2.解压下载的maven文件到任意指定文件夹 3.配置maven 右键“我的电脑” -> "属性" 在打开的属 ...
- XML文件数据操作
#region XML序列化文件和反序列化 /// <summary> /// 通用类的保存函数,可以将已经声明过可序列化的类以文件方式保存起来. /// 保存格式分为 XML明文式和 二 ...
- HDU 5033 Building
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5033 解题报告:在一条x轴上有n个建筑物,每个建筑物有一个高度h,然后现在有q次查询,查询的内容是假设 ...