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 Course Scheduling StrategyStep 1: Build Dependency GraphCreate adjacency list and calculate in-degrees for each courseMath3 monthsStart: Month 0Physics4 monthsStart: Month 3Engineer2 monthsStart: Month 7Timeline Analysis• Math (no prerequisites): Months 0-3• Physics (needs Math): Months 3-7• Engineering (needs Physics): Months 7-9🎯 Total Time: 9 MonthsCritical Path: Math → Physics → Engineering
Understanding the Visualization
1
Map Dependencies
Build a graph where edges represent prerequisite relationships
2
Find Starting Courses
Identify courses with no prerequisites - these can start immediately
3
Process in Order
Use topological sorting to process courses in dependency order
4
Calculate Times
For each course, its earliest start time is when all prerequisites are done
Key Takeaway
🎯 Key Insight: The minimum time is determined by the longest dependency chain (critical path), not the sum of all course times. Parallel execution of independent courses is automatically optimized through topological sorting.
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