Topological sort · Medium
A release orchestrator can start independent services together. Each service has a duration and must wait for all prerequisites.
Return the earliest total completion time or null for a dependency cycle.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ durations:integer[], dependencies:[before:integer,after:integer][] }
Input rules
- Duplicate edges are allowed.
What each field means
durations[i]— time required to deploy servicei.- Each
[before, after]dependency requiresbeforeto finish beforeafterstarts. - Independent services may run simultaneously; parallel capacity is unlimited.
Output
Return value and rules
- Return the earliest time at which all services can finish.
- Return
nullfor any dependency cycle, even if its durations are zero. - An empty deployment finishes at time
0.
Constraints
- 0 <= durations.length <= 100000; 0 <= dependencies.length <= 200000
- Durations in [0,1000000]; valid service indexes; unlimited parallel capacity.
Examples
Example 1
{
"dependencies": [
[
0,
2
],
[
1,
2
]
],
"durations": [
2,
5,
3
]
}Returns
8Example 2
{
"dependencies": [],
"durations": []
}Returns
0Required complexity
O(n + m) time and O(n + m) space