Coding Round 2
Mid-Level
programming
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 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. If it is impossible to finish all courses, return an empty array.
Input: numCourses = 4, prerequisites = [[2,0],[1,0],[3,1],[3,2]]
Output: [0,1,2,3]
Explanation: There are multiple correct orders, but one possible order is [0, 1, 2, 3].
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: []
Explanation: There's no way to complete the courses because they depend on each other.
Problem Statement
def findOrder(numCourses: int, prerequisites: List[List[int]]) -> List[int]:
Example 1
Input: numCourses = 4, prerequisites = [[2,0],[1,0],[3,1],[3,2]]
Output: [0,1,2,3]
Explanation: There are multiple correct orders, but one possible order is [0, 1, 2, 3].
Example 2
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: []
Explanation: There's no way to complete the courses because they depend on each other.
Constraints
- The input prerequisites is a graph, where the number of nodes is numCourses and the number of edges is the length of prerequisites.
Suggested Answer