Solution: Course Schedule II
Explore how to apply topological sort to determine a valid order for completing courses with prerequisites in the Course Schedule II problem. Understand building directed graphs, calculating in-degrees, identifying sources, and using breadth-first search for sorting. This lesson equips you with the skills to detect cycles and produce course sequences with efficient time and space complexity.
Statement
You are given n courses, labeled from 0 to n - 1. Some courses have prerequisites, which are provided as a list of pairs: prerequisites[i]
Your task is to determine a valid order in which you can complete all the courses and return it as an array of course labels.
If there are multiple valid orderings, you can return any of them.
If it’s impossible to finish all courses (due to a cycle in prerequisites), return an empty array.
Note: There can be a course in the
to range with no prerequisites.
Constraints:
Let be the number of courses.