01. Course Schedule II
β GFG solution to the Course Schedule II problem: find valid ordering of courses using topological sort with cycle detection. π
π§© Problem Description
π Examples
Example 1
Input: n = 3, prerequisites[][] = [[1, 0], [2, 1]]
Output: true
Explanation: To take course 1, you must finish course 0. To take course 2, you must finish course 1.
So the only valid order is [0, 1, 2].Example 2
Input: n = 4, prerequisites[][] = [[2, 0], [2, 1], [3, 2]]
Output: true
Explanation: Course 2 requires both 0 and 1. Course 3 requires course 2.
Hence, both [0, 1, 2, 3] and [1, 0, 2, 3] are valid.π Constraints
β
My Approach
Kahn's Algorithm (BFS Topological Sort)
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated