Minimum Time to Complete All Build Tasks with Dependencies
Trees & GraphsAuto-graded · 17 test casesLast updated
Tests ability to apply topological ordering (Kahn's algorithm) to schedule parallel tasks with durations and prerequisite chains, computing the earliest completion time.
Minimum Time to Complete All Build Tasks
You are managing a build system. There are n tasks numbered 0 through n - 1. Each task has a duration (the number of time units it takes to execute) and a list of prerequisite tasks that must fully complete before it can begin.
Tasks with no unfinished prerequisites can run in parallel starting at any moment their prerequisites are done.
Given:
durations: a list ofnpositive integers wheredurations[i]is the time to complete taski.prerequisites: a list of pairs[a, b]meaning taskbmust fully complete before taskacan start.
Return the minimum time (as an integer) at which all tasks have finished, assuming unlimited parallel workers.
If the dependency graph contains a cycle, return -1.
How time works
- Every task starts as soon as all its prerequisites are done.
- A task with no prerequisites can start at time
0. - The finish time of task
iis:(earliest start of i) + durations[i]. - The earliest start of task
iis:max(finish times of all prerequisites of i), or0if none. - The answer is
max(finish times of all tasks).
Constraints
1 <= n <= 20001 <= durations[i] <= 10000 <= len(prerequisites) <= 10000- Each pair
[a, b]satisfies0 <= a, b <= n - 1anda != b. - There may be duplicate edges in
prerequisites; treat them as a single dependency. - If
n == 1and there are no prerequisites, the answer is simplydurations[0]. - Return
-1if and only if the graph has at least one cycle.
Examples
Example 1
Input: minimum_build_time([3, 2, 5, 1], [[2, 0], [2, 1], [3, 2]])
Output: 9
Example 2
Input: minimum_build_time([4, 3, 6, 2, 5], [[1, 0], [2, 0], [3, 1], [3, 2], [4, 3]])
Output: 17
Example 3
Input: minimum_build_time([7], [])
Output: 7
Hints
Hint 1
Build a directed graph where an edge from b to a means 'b must finish before a starts'. Compute in-degrees for Kahn's algorithm.
Hint 2
Track the earliest finish time for each node. When you process a node in topological order, propagate its finish time to all its dependents: earliest_start[dependent] = max(earliest_start[dependent], finish[current]).
Hint 3
If after Kahn's BFS the number of processed nodes is less than n, a cycle exists — return -1. Otherwise return the maximum finish time across all nodes.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a critical path problem on a directed acyclic graph (DAG), solved with Kahn's topological sort (BFS-based).
Key Insight
Because tasks can run in parallel but must respect prerequisites, the earliest a task can start is determined by the latest finishing prerequisite — this is the classic critical-path calculation. The overall answer is the maximum finish time across all tasks.
Algorithm
-
Build the graph. For each prerequisite pair
[a, b], add a directed edgeb → a(b must finish before a starts). Deduplicate edges to avoid inflating in-degrees. -
Kahn's BFS. Maintain an
in_degreearray. Push all zero-in-degree nodes into a queue. Also maintainearliest_start[i]= the earliest time taskican begin (starts at 0 for all). -
Process in topological order. When node
nodeis dequeued:- Compute
finish[node] = earliest_start[node] + durations[node]. - For each outgoing neighbor, update
earliest_start[neighbor] = max(earliest_start[neighbor], finish[node]). - Decrement
in_degree[neighbor]; enqueue if it reaches 0.
- Compute
-
Cycle detection. If the number of processed nodes is less than
n, a cycle exists → return-1. -
Answer.
max(finish).
Correctness
Topological order guarantees that when we process a node, all its prerequisites have already been processed and earliest_start has been updated to reflect the correct maximum. This is equivalent to computing the longest weighted path to each node in the DAG.
Complexity
- Time: O(n + E) where E is the number of unique edges.
- Space: O(n + E) for the graph and auxiliary arrays.
from typing import List
from collections import deque
def minimum_build_time(durations: List[int], prerequisites: List[List[int]]) -> int:
n = len(durations)
adj = [[] for _ in range(n)]
in_degree = [0] * n
seen_edges = set()
for a, b in prerequisites:
if (b, a) not in seen_edges:
seen_edges.add((b, a))
adj[b].append(a)
in_degree[a] += 1
earliest_start = [0] * n
queue = deque()
for i in range(n):
if in_degree[i] == 0:
queue.append(i)
processed = 0
finish = [0] * n
while queue:
node = queue.popleft()
processed += 1
finish[node] = earliest_start[node] + durations[node]
for neighbor in adj[node]:
if finish[node] > earliest_start[neighbor]:
earliest_start[neighbor] = finish[node]
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
queue.append(neighbor)
if processed < n:
return -1
return max(finish)