BugForge
DSA

Service Deployment Order

Mediumtopological sortheap 00:00

There are n services labelled 0..n-1. Each pair [a, b] in deps means a must deploy before b. Return a valid deployment order; when several services are ready, deploy the smallest label first. Return [] if there's a cycle.

Signature

deployOrder(n: int, deps: int[][]) → int[]
Example 1
n = 4
deps = [[1,0],[2,0],[3,1],[3,2]]
output = [3,1,2,0]
3 first, then 1, 2, then 0
Example 2
n = 2
deps = [[0,1],[1,0]]
output = []
cycle → []

Constraints

  • 1 ≤ n ≤ 10^4
  • 0 ≤ |deps| ≤ 5·10^4
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).