The Parallel Courses III problem adds time costs: each course i takes time[i] months. Prerequisites must be completed before starting the next course. Find the minimum total time to complete all courses. This is the critical path problem on a DAG — find the longest weighted path. The array, graph, topological sort, and dynamic programming interview pattern is the core.
Citadel, Stripe, Snowflake, Amazon, TikTok, and Google ask this because it's the classic critical path method (CPM) problem used in project management. The minimum completion time = the longest weighted dependency chain. Topological sort with DP computes this efficiently.
Topological sort + DP. dp[i] = earliest completion time for course i = time[i] + max(dp[prereq] for all prereqs of i). Process courses in topological order. Answer = max(dp[i]) for all i.
n=3, relations=[(1,3),(2,3)], time=[3,2,5]. (Course 1 is prereq of 3, course 2 is prereq of 3.) dp[1]=3, dp[2]=2. dp[3]=5+max(3,2)=5+3=8. Answer = max(3,2,8) = 8.
n=5, relations=[(1,5),(2,5),(3,5)], time=[1,2,3,4,5]. dp[1]=1,dp[2]=2,dp[3]=3,dp[4]=4. dp[5]=5+max(1,2,3)=8. Answer=max(1,2,3,4,8)=8.
Critical path DP is essential for project scheduling problems. The DP recurrence: dp[i] = time[i] + max(dp[prereqs]). This is "longest path in a weighted DAG" — the dual of shortest path but computed via DP on topological order. Practice this on all "minimum time with dependencies" variants. It directly applies to build systems, CI/CD pipelines, and manufacturing scheduling.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Cost to Reach Destination in Time | Hard | Solve | |
| Sequence Reconstruction | Medium | Solve | |
| Strange Printer II | Hard | Solve | |
| Build a Matrix With Conditions | Hard | Solve | |
| Collect Coins in a Tree | Hard | Solve |