Parallel Courses III
Problem Statement
There are n courses, labelled 1 to n. Course i takes time[i − 1] months. Each pair relations[j] = [prevCourse, nextCourse] means prevCourse must be finished before nextCourse can start.
Any number of courses can run at the same time. A course starts the moment all its prerequisites are finished. Return the minimum number of months needed to finish every course.
n, the number of courses, relations, a list of pairs [prevCourse, nextCourse], and time, where time[i] is the length of course i + 1.
The earliest month in which every course is finished.
Courses 1 and 2 start at month 0 and finish at months 3 and 2. Course 3 waits for both, so it starts at month 3 and ends at 3 + 5 = 8.
Courses 1, 2 and 3 start at month 0. Course 4 waits for 3 and runs until 3 + 4 = 7. Course 5 waits for 1, 2, 3 and 4, so it starts at month 7 and ends at 7 + 5 = 12.
- • 1 ≤ n ≤ 5 × 10^4
- • 0 ≤ relations.length ≤ min(n · (n − 1) / 2, 5 × 10^4)
- • relations[j].length == 2
- • 1 ≤ prevCourse, nextCourse ≤ n
- • prevCourse ≠ nextCourse
- • All the pairs [prevCourse, nextCourse] are unique.
- • time.length == n
- • 1 ≤ time[i] ≤ 10^4
- • The graph is a directed acyclic graph.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each course, recurse into its prerequisites and keep the latest finish; no memory
Process courses in prerequisite order; each course keeps the latest finish of its prerequisites
A course finishes at (the latest finish of its prerequisites) + its own time. A course with no prerequisites finishes at its own time. Work this out for every course with a plain recursion that remembers nothing. The answer is the largest finish month.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.