dsa · medium
Topological Sort
GreyOrangeGraphQueue
n nodes 0..n-1, directed edges u -> v meaning u before v. Return a topological order, preferring the **smallest available** node at each step (Kahn + min-heap). Return [] if a cycle exists.
Arguments
n— number of nodes, labeled0..n-1edges— the edge list
**Example**
`` Input: 2 [[0,1]] Output: [0,1] ``
Constraints
1 <= n <= 100 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: 2 [[0,1]] Expected: [0,1]
Example 2
Input: 2 [[0,1],[1,0]] Expected: []