Course Schedule II
Description
Imagine you are designing the curriculum for an online coding course! Every module has prerequisites that must be completed before taking the next module. Your challenge is to find an order to take all the modules if possible.
You are given numCourses, a total number of modules (labeled from 0 to numCourses-1), and a list of prerequisites where prerequisites[i] = [ai, bi] indicates that you must take module bi before taking module ai.
Your task is to write a function course_schedule_order that returns the ordering of modules you should take.
If it's impossible to complete all courses, return an empty list. When several modules can be taken at the same time, take the one with the smallest number first.
Example
For example, if you have 4 courses, the modules and their dependencies can be completed as follows:
course_schedule_order(4, [[1,0],[2,0],[3,1],[3,2]]) # Output: [0, 1, 2, 3] course_schedule_order(2, [[1,0],[0,1]]) # Output: []
Let's Solve It!
Implement the function course_schedule_order(numCourses: int, prerequisites: List[List[int]]) -> List[int]
Hint
- Use a graph to represent courses and their prerequisites, and perform a topological sort. If there's a cycle in the graph, it's impossible to complete all courses.
Inputs
Arg 1:
4Arg 2:
[[1,0],[2,0],[3,1],[3,2]]Expected Output
[0, 1, 2, 3]Actual Output