This question evaluates competency in graph algorithms and scheduling, including dependency resolution, cycle detection, and aggregation of task durations; it belongs to the Coding & Algorithms domain and primarily tests practical application of algorithmic reasoning with conceptual understanding of precedence constraints.
You are given n courses labeled 1..n. Each course i takes time[i] units of time to complete.
You are also given prerequisite relations prereqs, where each pair [a, b] means course a must be completed before course b can be taken.
Courses are taken in batches (rounds/semesters) under the following rule:
max(time[c])
over all courses
c
taken in that batch.
Return the minimum total time required to finish all courses.
If the prerequisite graph contains a cycle (so it is impossible to finish all courses), return -1.
Suppose prerequisites imply two batches: first take courses {1, 3}, then take {2, 4} (e.g., edges 1 -> 2 and 3 -> 4).
time = [1, 10, 10, 1]
for courses
1..4
max(1, 10) = 10
max(10, 1) = 10
20
How would your approach change if courses can start immediately when their prerequisites are satisfied, without waiting for the rest of the current batch to finish (i.e., no batch barrier; courses can overlap asynchronously)? In that variant, return the minimum total completion time or -1 if a cycle exists.
1 <= n <= 2 * 10^5
0 <= prereqs.length <= 2 * 10^5
1 <= time[i] <= 10^9