Parallel Courses III - Problem
Parallel Courses III is an advanced scheduling problem where you need to find the minimum time to complete all university courses given prerequisite constraints.

You have n courses labeled from 1 to n, where each course has:
• A specific duration (in months) to complete
• Zero or more prerequisite courses that must be finished first

Key Rules:
• You can start any course immediately if all its prerequisites are met
• Multiple courses can be taken simultaneously (parallel processing)
• The goal is to minimize total completion time

Input: Number of courses n, prerequisite relationships relations[][], and course durations time[]
Output: Minimum months needed to complete all courses

Think of this as optimizing a project timeline where some tasks depend on others, but independent tasks can run in parallel.

Input & Output

example_1.py — Linear Chain
$ Input: n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
Output: 8
💡 Note: Courses 1 and 2 can start simultaneously and finish at months 3 and 2 respectively. Course 3 can start at month 3 (when both prerequisites are done) and finishes at month 3+5=8.
example_2.py — Single Course
$ Input: n = 1, relations = [], time = [1]
Output: 1
💡 Note: Only one course with no prerequisites, takes 1 month to complete.
example_3.py — Complex Dependencies
$ Input: n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
Output: 12
💡 Note: Course 3 finishes at month 3, course 4 at month 7 (3+4), and course 5 at month 12 (7+5). Courses 1 and 2 finish earlier but don't affect the critical path.

Constraints

  • 1 ≤ n ≤ 5 × 104
  • 0 ≤ relations.length ≤ min(5000, n × (n - 1) / 2)
  • relations[j].length == 2
  • 1 ≤ prevCoursej, nextCoursej ≤ n
  • prevCoursej ≠ nextCoursej
  • All the pairs [prevCoursej, nextCoursej] are unique
  • time.length == n
  • 1 ≤ time[i] ≤ 104
  • The given graph is a directed acyclic graph (DAG)

Visualization

Tap to expand
Parallel Courses III - Optimal Solution INPUT Course Dependency Graph 1 t=3 2 t=2 3 t=5 n = 3 relations = [[1,3],[2,3]] time = [3, 2, 5] 1-->3 and 2-->3 prereqs ALGORITHM STEPS 1 Build Graph Create adjacency list, compute in-degrees 2 Init dist[] Array dist[i] = time[i] for courses with no prereqs 3 Topological Sort + DP BFS: dist[v] = max(dist[v], dist[u] + time[v]) 4 Find Maximum Return max(dist[]) DP Calculation: dist[1] = 3 (no prereq) dist[2] = 2 (no prereq) dist[3] = max(3,2) + 5 = 8 Critical path: 1-->3 FINAL RESULT Timeline (months) 0 4 8 Course 1 C2 Course 3 Output: 8 Minimum months to complete all courses OK Key Insight: This problem combines Topological Sort with Dynamic Programming to find the longest path in a DAG. The critical path determines minimum completion time. Parallel execution means we take MAX of prerequisites, not SUM. Time complexity: O(V + E) where V = courses, E = prerequisite relations. TutorialsPoint - Parallel Courses III | Optimal Solution (Topological Sort + DP)
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
52.0K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen