Loading...
You are given an integer n and an array requirements, where requirements[i] = [a, b] means task b must be completed before task a can start. The n tasks are labeled 0 to n - 1.
Return a valid order in which to complete all n tasks. Because more than one valid order can exist, return the lexicographically smallest one: at each step, among the tasks whose requirements are all already met, complete the one with the smallest label.
If it is impossible to complete all tasks, because some chain of requirements loops back on itself, return an empty array.
Input: n = 2, requirements = [[1,0]]
Output: [0,1]
Explanation: Task 0 has no requirement, so it comes first; task 1 requires 0.
Input: n = 4, requirements = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3]
Explanation: Task 0 first (nothing required). Then, among 1 and 2 (both now
available), the smaller label 1 goes first, then 2, then 3.
Input: n = 1, requirements = []
Output: [0]
Explanation: A single task with no requirements.
n ≤2000requirements.length ≤5000requirements[i].length =2nrequirements[i] are unique.Click "Run" to test with sample cases or "Submit" to run all tests.