Parallel Courses II - Problem
Course Scheduling Optimization Challenge

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
Course Scheduling: Skill Tree AnalogySemester 1Course 2Course 3Course 1Course 4Semester 2Course 2Course 3Course 1Course 4Semester 3Course 2Course 3Course 1Course 4๐ŸŽฎ Game Analogy:๐Ÿ”’ Locked Skills: Prerequisites not met (dashed border)๐ŸŸก Available Skills: Can learn this semester (orange border)โœ… Learned Skills: Already completed (green fill)๐Ÿ’ก Strategy: Each semester, choose up to k available courses๐ŸŽฏ Goal: Find minimum semesters to unlock all courses
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

n
2n
โš  Quadratic Growth
Space Complexity
O(2^n)

Memoization table stores results for up to 2^n different states of completed courses

n
2n
โš  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)
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 31
56.2K Views
Medium 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