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. linux 下 ls 文件夹和文件没有颜色的解决办法

    .bashrc 中加入 alias ls="ls --color"

  2. redis+Keepalived实现Redis主从复制

    redis+Keepalived实现Redis主从复制: 环境:CentOs6.5Master: 10.10.10.203Slave:   10.10.10.204Virtural IP Addres ...

  3. Pycharm 使用

    Pycharm基本使用http://edu.51cto.com/index.php?do=lession&id=118722   Pycharm的基本使用 在Pycharm下为你的Python ...

  4. js实现-下拉列表左右选择

    下拉列表左右选择          * 下拉选择框               <select>                     <option>111</opt ...

  5. aop实现日志(转)

    文章来至于http://www.thinksaas.cn/group/topic/341436/ 第一:>>在spring的配置文件里增加以下配置 <!-- 支持 @AspectJ ...

  6. STM32向量表详细分析

    预备知识: DCD指令:用于分配一片连续的字存储单元(32bit),并将表达式的值初始化给该字存储单元,类似于C中定义数组并初始化.比如: DCD 0 的意思是:分配一个字存储单元,并将该单元初始化为 ...

  7. xinetd

    最简安装centos6.4时,xinetd服务是没有安装的,只是在/etc下有xinetd.d目录, 没有xinetd.conf这个配置文件 xinetd is a secure replacemen ...

  8. Extjs3 + swfUpload实现多文件上传控件

    要在ExrtJS框架实现选择多文件上传,FileUploadField已经无法满足需求,所以采用了 swfUpload上传控件,上传窗口如下: 多选文件进行上传(其实是每个文件异步上传),可以中途停止 ...

  9. nyoj 289 苹果 动态规划 (java)

    分析:0-1背包问题 第一次写了一大串, 时间:576  内存:4152 看了牛的代码后,恍然大悟:看来我现在还正处于鸟的阶段! 第一次代码: #include<stdio.h> #inc ...

  10. Javascript软键盘设计

    国内大多数网站的密码在网络传输过程中都是明文的,我们目前正在做的产品也是这样的情形,这正常吗? 大家都偷懒?不重视安全?各人持有观点,有人认为明文传输并不是想象中的那么可怕,事实上正常情况下这些报文你 ...