Parallel Courses II - Problem
Course Scheduling Optimization Challenge
Imagine you're a university student planning your course schedule efficiently! You have
You're given a list of prerequisite relationships in the
Here's the constraint: you can only take at most
Your goal: Find the minimum number of semesters needed to complete all courses.
Example: With courses [1,2,3,4], relations [[2,1],[3,1],[1,4]], and k=2, you need 3 semesters: first take courses 2&3, then course 1, finally course 4.
Imagine you're a university student planning your course schedule efficiently! You have
n courses numbered from 1 to n, but there's a catch - some courses have prerequisites that must be completed first.You're given a list of prerequisite relationships in the
relations array, where relations[i] = [prevCourse, nextCourse] means you must complete prevCourse before taking nextCourse.Here's the constraint: you can only take at most
k courses per semester, and you must have completed all prerequisites in previous semesters.Your goal: Find the minimum number of semesters needed to complete all courses.
Example: With courses [1,2,3,4], relations [[2,1],[3,1],[1,4]], and k=2, you need 3 semesters: first take courses 2&3, then course 1, finally course 4.
Input & Output
example_1.py โ Basic Case
$
Input:
n = 4, relations = [[2,1],[3,1],[1,4]], k = 2
โบ
Output:
3
๐ก Note:
Semester 1: Take courses 2 and 3 (no prerequisites). Semester 2: Take course 1 (prerequisites 2,3 satisfied). Semester 3: Take course 4 (prerequisite 1 satisfied). Total: 3 semesters.
example_2.py โ Chain Dependencies
$
Input:
n = 5, relations = [[2,1],[3,2],[4,3],[1,5]], k = 2
โบ
Output:
4
๐ก Note:
Semester 1: Take courses 3,4. Semester 2: Take course 2. Semester 3: Take course 1. Semester 4: Take course 5. The chain dependencies force a specific order despite k=2.
example_3.py โ Independent Courses
$
Input:
n = 6, relations = [], k = 3
โบ
Output:
2
๐ก Note:
No prerequisites means all courses are independent. Semester 1: Take any 3 courses. Semester 2: Take remaining 3 courses. Total: 2 semesters.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Available Skills
Find courses with no unfulfilled prerequisites (skills you can learn now)
2
Choose Skill Combination
Select up to k courses to take this semester (spend your skill points)
3
Unlock Dependencies
Completing courses unlocks new courses that depend on them
4
Progress to Next Level
Move to next semester with updated available courses
5
Optimize Path
Use DP to find the shortest path to unlock all skills
Key Takeaway
๐ฏ Key Insight: Use dynamic programming with bitmasks to efficiently track which courses are completed, while trying all valid combinations of available courses each semester to find the optimal scheduling path.
Time & Space Complexity
Time Complexity
O(2^n * n * C(n,k))
We have 2^n possible states, for each state we check n courses, and generate up to C(n,k) combinations to try
โ Quadratic Growth
Space Complexity
O(2^n)
Memoization table stores results for up to 2^n different states of completed courses
โ Quadratic Space
Constraints
- 1 โค n โค 15
- 1 โค k โค n
- 0 โค relations.length โค n ร (n-1) / 2
- relations[i].length = 2
- 1 โค prevCoursei, nextCoursei โค n
- prevCoursei โ nextCoursei
- All the pairs [prevCoursei, nextCoursei] are unique
- The given graph is a valid DAG (no cycles)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code