Course Schedule II 解答
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 解答的更多相关文章
- 【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 ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [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 ...
随机推荐
- qcow2 raw vhd 虚拟磁盘转换
Centos-6.4-x86_64_Ruiy.vhd: Microsoft Disk Image, Virtual Server or Virtual PC
- apache 配置网站目录,虚拟目录,新端口
1 配置网站目录,以ubuntu为例 1)打开apache的默认配置文件夹:cd /etc/apache2/sites-available 2)打开配置文件,本机为sudo vi 000-defau ...
- TypeScript 素描 - 模块解析、声明合并
模块解析 模块解析有两种方式 相对方式 也就是以/或 ./或-/开头的,比如import jq from "/jq" 非相对方式 比如 import model from ...
- IOS 监听通讯录是否改变
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABAddressBookRegist ...
- [think in java]知识点学习
java中 全部数值都有正负号,不存在无符号整数. java中的基本类型存储在堆栈中. 其它对象存储在堆中. java确保数组会被初始化,并且不能在它的范围之外被訪问. 下面代码在c和c++中是合法的 ...
- freemarker报错之三
1.错误描写叙述 Expression students is undefined on line 30, column 24 in student.ftl. The problematic inst ...
- Working with Numbers in PL/SQL(在PL/SQL中使用数字)
This article gives you all the information you need in order to begin working with numbers in your P ...
- 编程算法 - 圆圈中最后剩下的数字(递推公式) 代码(C++)
圆圈中最后剩下的数字(递推公式) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 0,1...,n-1这n个数字排成一个圆圈, 从数字0開始 ...
- java第二周学习日记
day01 1.抽象类 (1)抽象类的应用场景: 我们在描述一类事物的时候,发现该种事物确实存在着某种行为,但是这种行为目前不是具体的,那么我们可以抽取这种行为的声明,但是不去实现该种行为,这时候这种 ...
- linux 系统监控系列之vmstat
vmstat的官方定义是:vmstat - Report virtual memory statistics,即虚拟内存的统计. 先来追根溯源: 什么是虚拟内存? 答:虚拟内存就是磁盘上虚拟出来可以当 ...