Course Schedule
Welcome to the Course Schedule challenge! Imagine you're in charge of organizing classes for a magical school. The school has several courses, and some courses need to be completed before others. Your task is to determine the order in which the courses should be taken.
You will be given two things:
- The total number of courses (
numCourses). - A list of course prerequisites (
prerequisites), where each item is a pair [a, b] indicating that coursebmust be completed before coursea.
Your task is to write a Python function that returns a list representing the order in which the courses should be taken. If there's no valid order, return an empty list.
Examples
-
Example 1:
numCourses = 2 prerequisites = [[1,0]] course_schedule(numCourses, prerequisites) # Output: [0, 1]Here, you can take course 0 first, followed by course 1.
-
Example 2:
numCourses = 4 prerequisites = [[1,0],[2,0],[3,1],[3,2]] course_schedule(numCourses, prerequisites) # Output: [0, 1, 2, 3]The courses can be taken in the order: 0, 1, 2, 3 as they satisfy all prerequisite conditions.
-
Example 3:
numCourses = 2 prerequisites = [[1,0],[0,1]] course_schedule(numCourses, prerequisites) # Output: []There is no way to complete the courses because of a circular dependency, so we return an empty list.
Good luck on your magical scheduling journey!
Inputs
Arg 1:
2Arg 2:
[[1,0]]Expected Output
[0, 1]Actual Output