← 返回 amazon 的题目列表Course Schedule II: Find Valid Course Order Topological Sort
类型:online_judge
amazon
Given a directed graph representing course prerequisites with n courses and some prerequisite pairs, determine the order in which all courses should be taken. If it is not possible, return an empty list.
Input:
n: Total number of courses.
prerequisites: An array where each element is [ai, bi] indicating course ai depends on course bi.
Output:
A list representing the course order or an empty list.
Example:
Input: n = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Input: n = 2, prerequisites = [[1,0],[0,1]]
Output: []
Constraints:
The total number of courses n is a positive integer.
prerequisites is a 2D array of length m where ai and bi are integers between 0 and n-1.
Example
Input
4
[[1,0],[2,0],[3,1],[3,2]]