Question

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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

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 the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

Solution

Similar with "Course Schedule", the only difference is that we need to record path.

Note: an empty array is the array with length = 0.

 public class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
// This problem is to print one possible topological sort result
// First, we need to construct a directed graph in the form of adjacency list
List<Integer>[] adjacencyList = new ArrayList[numCourses];
int[] result = new int[numCourses];
int[] degree = new int[numCourses];
Arrays.fill(degree, 0);
for (int j = 0; j < numCourses; j++) {
List<Integer> tmpList = new ArrayList<Integer>();
tmpList.add(j);
adjacencyList[j] = tmpList;
}
int length = prerequisites.length;
for (int j = 0; j < length; j++) {
int[] pair = prerequisites[j];
adjacencyList[pair[1]].add(pair[0]);
degree[pair[0]]++;
} // queue is to store nodes with 0 in-degree
Queue<Integer> queue = new LinkedList<Integer>();
for (int j = 0; j < numCourses; j++) {
if (degree[j] == 0)
queue.add(j);
}
if (queue.size() == 0)
return new int[0];
int i = 0; // begin bfs
while (queue.size() > 0) {
int current = queue.remove();
result[i] = current;
List<Integer> currentList = adjacencyList[current];
for (int j = 1; j < currentList.size(); j++) {
int tmp = currentList.get(j);
degree[tmp]--;
if (degree[tmp] == 0)
queue.add(tmp);
}
i++;
}
if (i < numCourses)
return new int[0];
return result;
}
}

Course Schedule II 解答的更多相关文章

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

  2. [Leetcode Week4]Course Schedule II

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

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

  4. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  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. margin:0 auto 与 text-align:center 的区别(转载)

    摘自:http://www.cnblogs.com/zhwl/p/3529473.html 基本概念: 1.text-align: 属性规定元素中的文本的水平对齐方式;   该属性通过指定行框与哪个点 ...

  2. 【布艺DIY】 零基础 做包包 2小时 就OK!_豆瓣

    [布艺DIY] 零基础 做包包 2小时 就OK!_豆瓣 [布艺DIY] 零基础 做包包 2小时 就OK!

  3. 《UNIX环境高级编程》笔记--chown,fchown和lchown函数

    这三个函数都是用来更改文件的用户ID和组ID的,函数的定义如下: #include <unistd.h> int chown(const char *pathname, uid_t own ...

  4. hdu 4405 Aeroplane chess(概率+dp)

    Problem Description Hzz loves aeroplane chess very much. The chess map contains N+ grids labeled to ...

  5. 初学github

    在公司一直用的SVN做版本管理,倒也没什么问题.最近想自己在家写点东西,上班的时候又想偷偷地写.代码经常在两个地方同步,很是辛苦.反正写的只是一些用来学习测试的代码,干脆放到github上. 1.登录 ...

  6. ABAP - 日期格式转换 &amp; ABAP经常使用日期处理函数

    ABAP - 日期格式转换 如今提供下面一些日期格式转换的函数: Below are several FMs which can be used to convert date format. 1. ...

  7. POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

    解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...

  8. LabView培训

    labview基础到高级官方超全完整视频教程包括数据采集,其他模块的部 分教程,废话不多说. 免费下载地址在结尾. LabVIEW 本事编程(低级)培训LabVIEW作为前辈的图形化编程言语斥地环境, ...

  9. Oracle字符集查看

    Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE 支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.它使数据库工具,错误消息,排序次序,日期 ...

  10. libxml两种换行方法

    好久没上来留下一些记录了,可能是太忙,又或者是过于慵懒便疏于整理. libxml是一个开源的库,linux下解析xml文件经常用到,进行一些创读增删的操作. 最开始接触的时候,看到了一个简明易懂的&l ...