Course Schedule II
MediumP2Coding
There are numCourses courses labelled 0..numCourses - 1. Each entry [a, b] of prerequisites means you must take course b before course a.
Return an order in which you can take all courses. Many orders can be valid, so return the lexicographically smallest one: whenever several courses are available, take the one with the smallest label. If it is impossible (there is a cycle), return [].
Example 1
- Input
- numCourses = 2, prerequisites = [[1,0]]
- Output
- [0,1]
Example 2
- Input
- numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
- Output
- [0,1,2,3]
- Why
- Course 0 first; then 1 and 2 are available and 1 is smaller; then 2; then 3.
Example 3
- Input
- numCourses = 2, prerequisites = [[0,1],[1,0]]
- Output
- []
Constraints
- 1 <= numCourses <= 2000
- 0 <= prerequisites.length <= 5000
- 0 <= a, b < numCourses, a != b
- Pairs are distinct.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.