FunctionRacerProblemsRace

Build Order Exists

MediumGraphsPython, JavaScript, TypeScript, Java, C++

A project has n build steps numbered 0 to n - 1. before is a list of pairs [a, b] meaning step a must be completed before step b can start. The same pair may appear more than once.

Return true if all n steps can be arranged in one sequence that respects every pair, and false if the requirements contradict each other. With no pairs the answer is true.

Example 1

n 3
before [[0,1],[1,2]]
returns true

The order 0, 1, 2 respects both pairs.

Example 2

n 3
before [[0,1],[1,2],[2,0]]
returns false

Step 0 must come before 1, 1 before 2, and 2 before 0, which is impossible.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems