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的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. [Leetcode Week4]Course Schedule II

    Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...

  4. 【刷题-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 ...

  5. [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 ...

  6. 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 ...

  7. LeetCode Course Schedule II

    原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...

  8. FB面经prepare: task schedule II

    followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...

  9. 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 ...

随机推荐

  1. Effective Objective-C 2.0 — 第三条:多用字面量语法,少用与之等价的方法

    第三条:多用字面量语法,少用与之等价的方法 几个类:NSString  NSNumber  NSArray  NSDictionary 字面量语法是一种语法糖(syntactic sugar) NSS ...

  2. CSS 改变文本选中颜色

    改变文字颜色 ::selection {    background: #f88;    text-shadow: none;    color: #000;}::-moz-selection {  ...

  3. PPTP(Point to Point Tunneling Protocol),即点对点隧道协议。

    PPTP PPTP(Point to Point Tunneling Protocol),即点对点隧道协议.该协议是在PPP协议的基础上开发的一种新的增强型安全协议,支持多协议虚拟专用网(VPN),可 ...

  4. 【9-20】vimtutor学习笔记

    第一节 ghjk移动光标 :q!:强制退出vim x:删除光标处的字符 i:在光标处插入 A:附加文本 :wq:保存文档并退出 第二节 dw:删除一个单词 d$:删除至行尾 de:删除光标处到该单词结 ...

  5. 【8-19】java学习笔记01

    JDK API文档 java SE 8 API文档:http://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downl ...

  6. 【CISP笔记】安全漏洞与恶意代码(1)

    恶意代码 类型二进制代码.脚本语言.宏语言等表现形式病毒.蠕虫.后门程序.木马.流氓软件.逻辑炸弹等 恶意代码的传播方式(1)移动存储 自动播放功能.Windows默认.自动执行autorun.inf ...

  7. WCF 已知类型和泛型解析程序 KnownType

    数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...

  8. 大数据BI积累

    http://blog.csdn.net/wyzxg/article/category/535869 设计论文:http://www.doc88.com/p-3877368345851.html 自动 ...

  9. 关于Sublime text 的PHP编译环境配置的问题

    前一段时间终于装上了传说中的代码编辑神器====>Sublime Text ,一打开便爱不释手,于是在网上找PHP的配置方案和插件,所有的一切都搞定了,可就是编译的时候没有显示,也没有提示,熬了 ...

  10. 2015多校1006.First One

    First One Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...