Parallel Courses - Problem

Imagine you're a university student trying to graduate as quickly as possible! ๐ŸŽ“ You have n courses labeled from 1 to n that you need to complete, but there's a catch - some courses have prerequisites.

You're given an array relations where relations[i] = [prevCoursei, nextCoursei] means you must complete prevCoursei before you can take nextCoursei. The good news? In any semester, you can take unlimited courses as long as you've completed all their prerequisites in previous semesters.

Goal: Find the minimum number of semesters needed to complete all courses. If it's impossible due to circular dependencies (like Course A requires Course B, but Course B requires Course A), return -1.

Example: If you have 3 courses with relations [[1,3],[2,3]], you can take courses 1 and 2 in semester 1, then course 3 in semester 2, requiring 2 semesters total.

Input & Output

example_1.py โ€” Basic Prerequisites
$ Input: n = 3, relations = [[1,3],[2,3]]
โ€บ Output: 2
๐Ÿ’ก Note: Course 3 requires both courses 1 and 2 as prerequisites. In semester 1, we can take courses 1 and 2 simultaneously since they have no prerequisites. In semester 2, we can take course 3 since its prerequisites (1 and 2) are now satisfied.
example_2.py โ€” Impossible (Cycle)
$ Input: n = 3, relations = [[1,2],[2,3],[3,1]]
โ€บ Output: -1
๐Ÿ’ก Note: There's a circular dependency: course 1 requires course 3, course 3 requires course 2, and course 2 requires course 1. This creates a cycle, making it impossible to complete all courses.
example_3.py โ€” Linear Chain
$ Input: n = 4, relations = [[1,2],[2,3],[3,4]]
โ€บ Output: 4
๐Ÿ’ก Note: Courses form a linear chain: 1 โ†’ 2 โ†’ 3 โ†’ 4. Each course can only be taken after completing the previous one, so we need 4 semesters total.

Constraints

  • 1 โ‰ค n โ‰ค 5000
  • 0 โ‰ค relations.length โ‰ค 5000
  • relations[i].length == 2
  • 1 โ‰ค prevCoursei, nextCoursei โ‰ค n
  • prevCoursei โ‰  nextCoursei
  • All prerequisite relationships are distinct

Visualization

Tap to expand
Course Scheduling VisualizationSemester 1MathCSSemester 2AlgSemester 3AIKey Insight: Topological Orderingโ€ข Courses with no prerequisites can be taken simultaneouslyโ€ข Each semester unlocks new courses for the nextโ€ข Cycles make graduation impossibleโ€ข BFS naturally processes courses level by levelAlgorithm: Kahn's Topological SortTime: O(V + E) | Space: O(V + E) | Optimal for this problem!
Understanding the Visualization
1
Map Dependencies
Create a prerequisite map showing which courses must be completed before others
2
Find Starting Courses
Identify courses with no prerequisites - these can be taken in the first semester
3
Semester Planning
Each semester, take all courses whose prerequisites are satisfied
4
Progress Tracking
After completing courses, update which new courses become available
5
Cycle Detection
If no courses can be taken but some remain, there's a circular dependency
Key Takeaway
๐ŸŽฏ Key Insight: This is essentially finding the longest path in a DAG - each semester represents a level in the topological ordering, and we can process multiple courses per level simultaneously!
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
47.8K Views
Medium Frequency
~15 min Avg. Time
1.5K 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