Dodomeki
Sign in

78. Course Order

Medium

Given total courses and pairs [curso, requisito]. Return a valid order to complete all courses, or [] if there is a cycle. When multiple courses are available at the same time, always choose the course with the smallest number; this rule applies at every step.

Example 1
Input: total = 4, requisitos = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3]
Example 2
Input: total = 2, requisitos = [[1,0],[0,1]]
Output: []
Constraints
  • 0 <= total <= 2000
  • 0 <= requisitos.length <= 5000
  • Os índices dos cursos são válidos.
GraphsBFSHeap
Caso 1
total =
4
requisitos =
[[1,0],[2,0],[3,1],[3,2]]
Expected =
[0,1,2,3]
Caso 2
total =
5
requisitos =
[[1,0],[2,0],[3,1],[3,2]]
Expected =
[0,1,2,3,4]

Run checks these 2 cases. Submit runs all 6, including hidden ones.