Standard Bar Graphs · Topo Sort Problems O(N + E) · O(N + E)
With the feasibility question settled, the registrar now needs an actual timetable: a listing of every training pass in an order that never violates a single requirement. Many legal timetables may exist — any one is acceptable — but circular requirements still force the registrar to publish nothing at all.
Input: An integer numCourses and a list prerequisites where each pair [a, b] means course b must be taken before course a.
Output: Return one valid ordering of all courses, or an empty array if no valid order exists.
1 <= numCourses <= 20000 <= prerequisites.length <= 5000All prerequisite pairs are uniqueInput: {"numCourses":4,"prerequisites":[[1,0],[2,0],[3,1],[3,2]]}
Output: [0,2,1,3]
Course 0 unlocks both 1 and 2; this queue order happens to admit 2 before 1.
Input: {"numCourses":2,"prerequisites":[[1,0],[0,1]]}
Output: []
The circular requirement means no timetable can ever be published.