Lexicographically smallest task order

Write codeMedium30 minGoogle style

Your task

You are given n tasks labeled 0..n-1 and a list of prerequisite pairs [a, b] meaning task b must be completed before task a. Return the lexicographically smallest valid ordering of all tasks (as a list). When multiple tasks are simultaneously available, always pick the one with the smallest label. If no valid ordering exists because of a cycle, return an empty list.

Examples

smallest_topo_order(n: int, prereqs: list[list[int]]) -> list[int]
Example 1
Inputsmallest_topo_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])
Output[0, 1, 2, 3]