Explanation:Course 1 and 2 require course 0. Course 3 requires both 1 and 2. Both courses 1 and 2 require course 0. A possible valid order is 0, 2, 1, 3. Another is 0, 1, 2, 3.
Example 3:
Input:numCourses = 1, prerequisites = []
Output:[0]
Explanation:There is only one course and no prerequisites, so taking course 0 is the only step.
Explanation:Course 0 requires 1, 1 requires 2, and 2 requires 0. This forms a cycle (0 -> 1 -> 2 -> 0), so no valid order exists.
Constraints:
•
•
•
•
•
•
Time Complexity
O(V + E)
Space Complexity
O(V + E)
Step 1 / 38
Speed: 1.0x
Courses State
0
1
2
3
In Cycle (Active DFS)
Visited (Completed)
Output Topological Ordering
Output array is empty
Current Step Explanation:
We are given 4 courses. The prerequisites array defines dependencies. We need to find a valid ordering to take all courses. If impossible, return an empty array.