Skip to the code
cf.
+10 XP

#439 Can the Build Finish?

easy

A monorepo has n packages numbered 0 to n - 1. Each deps[i] = [a, b] means package a depends on package b, so b must be built before a.

Write canFinish(n, deps) that returns true if there is some order that builds every package after everything it depends on, and false if the dependencies go in a circle.

Example 1

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

Build 0, then 1, then 2.

Example 2

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

Each package waits for the other, so neither can start.

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 solves the problem when they all pass.

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