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. map reduce

    作者:Coldwings链接:https://www.zhihu.com/question/29936822/answer/48586327来源:知乎著作权归作者所有,转载请联系作者获得授权. 简单的 ...

  2. zoj3811 Untrusted Patrol (dfs)

    2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...

  3. zepto-selector.js简单分析

    zepto 的selector模块是对jquery扩充选择器(jquery-selector-extensions)的部分实现.比如这样的选择方法:$('div:first') 和 el.is(':v ...

  4. SQL存储过程来调用webservice

    如果用存储过程来调用webservice 那存储过程的功能感觉能做好多事情了? 别自欺欺人了.那些功能还是webservice来实现的... 完整的webservice代码:(也是默认的,新建.asm ...

  5. windows2003最详细的安装操作步骤.(最详细)

    以下为windows2003的安装操作步骤,由于安装操作步骤较多,安装可能需要一定的实际安装经验.安装时请参照此文档一步步完成安装. 一.首先准备好Windows2003安装光盘CD1,将CD1光盘放 ...

  6. Vno博客样式分享

    不知不觉有一年多没有更新博客了,还是几位园友因为喜欢这套博客样式发了消息,否则我都快忘记自己还有一个博客了,哈哈. 言归正传,这套博客样式是当时闲来无事copy的iOS界喵神的博客Vno,确实很漂亮, ...

  7. [机器学习]信息&熵&信息增益

    关于对信息.熵.信息增益是信息论里的概念,是对数据处理的量化,这几个概念主要是在决策树里用到的概念,因为在利用特征来分类的时候会对特征选取顺序的选择,这几个概念比较抽象,我也花了好长时间去理解(自己认 ...

  8. 示波器trigger的使用方法

    背景: 下位机有俩个IO口设置为外部中断——边沿触发.低电平有效.因此我需要抓取下降沿波形,但低电平时间很短,手动暂停抓取不仅不科学还费力,那么该如何准确的抓取到呢?最好的办法是使用示波器的trige ...

  9. Swift2.1 语法指南——自动引用计数

    原档: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programm ...

  10. CentOS7安装elk

    192.168.161.128 elk.test.com jdk-8u102-linux-x64.rpm elasticsearch-2.3.3.rpm kibana-4.5.1-1.x86_64.r ...