Skip to the code
+25 XP

#442 Fewest Build Rounds

medium

A CI system builds packages in rounds. In each round, it builds every package whose dependencies were all built in earlier rounds, all at the same time. Packages are numbered 0 to n - 1, and deps[i] = [a, b] means a depends on b.

Write buildRounds(n, deps) that returns how many rounds it takes to build every package, or -1 if the dependencies contain a circle.

Example 1

Input: n = 4, deps = [[1,0],[2,0],[3,1],[3,2]]
Output: 3

Round 1 builds 0. Round 2 builds 1 and 2 together. Round 3 builds 3.

Example 2

Input: n = 3, deps = []
Output: 1

Nothing depends on anything, so everything builds in round 1.

Constraints

  • 1 <= n <= 10000
  • 0 <= deps.length <= 50000
  • 0 <= a, b < n
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).