114. Compilation Queue
Medium
A platform needs to compile n modules numbered from 0 to n - 1. Some compilations have dependencies: each pair [a, b] in dependencias means module a can only be compiled after module b.
Return a valid compilation order. At each step, among the modules that can already be compiled, always choose the module with the smallest number. If a dependency cycle makes it impossible to compile every module, return an empty list.
Example 1
Input: n = 4, dependencias = [[1, 0], [2, 0], [3, 1]]
Output: [0, 1, 2, 3]
Explanation: After compiling module 0, modules 1 and 2 are ready; choose 1 first.
Example 2
Input: n = 3, dependencias = [[0, 1], [1, 2], [2, 0]]
Output: []
Explanation: The dependencies form a cycle, so no valid order exists.
Constraints
- 1 <= n <= 10^5
- 0 <= dependencias.length <= 2 * 10^5
- Each element of dependencias contains exactly two integers [a, b].
- 0 <= a, b < n
GraphsBFS
Saved in this browser
Caso 1
n =
4
dependencias =
[[1,0],[2,0],[3,1]]
Expected =
[0,1,2,3]
Caso 2
n =
3
dependencias =
[[0,1],[1,2],[2,0]]
Expected =
[]
Run checks these 2 cases. Submit runs all 11, including hidden ones.