Build Order Exists
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
1 <= n <= 500 <= len(before) <= 100before[i] = [a, b]with0 <= a, b < nanda != b
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Calm SquaresMedium
- Cheapest Toll TripMedium
- Connect All OfficesMedium
- Count the Shortest RoutesMedium
- Forced SidesMedium
- Largest PatchMedium
- Longest TrailMedium
- Lowest High PassMedium