[Hard] 1136. Parallel Courses
You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCoursei, nextCoursei], representing a prerequisite relationship between course prevCoursei and course nextCoursei: course prevCoursei has to be taken before course nextCoursei.
In one semester, you can take any number of courses as long as you have taken all the prerequisites for the course you are taking.
Return the minimum number of semesters needed to take all courses. If there is no way to take all the courses, return -1.
Thinking Process
- Longest Path in DAG: Minimum semesters = length of longest path
- Each node in path must be taken in sequence
- Longest path determines minimum semesters needed
0= unvisited
- Define state: what subproblem does
dp[i](ordp[i][j]) represent? - Recurrence: how does the answer build from smaller indices?
- Base cases first; optimize space if only prior row/layer is needed.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| 1D DP (this problem) | O(n) | O(n) or O(1) | Linear recurrence |
| 2D DP | O(nm) | O(nm) or O(n) | Grid or two-sequence problems |
| State machine DP | O(n) | O(1) | Buy/sell, hold/not-hold states |
| Memoization (top-down) | Same as DP | O(n) | Recursive + cache |
Examples
Example 1:
Input: n = 3, relations = [[1,3],[2,3]]
Output: 2
Explanation: The figure above represents the given graph.
In the first semester, you can take courses 1 and 2.
In the second semester, you can take course 3.
Example 2:
Input: n = 3, relations = [[1,2],[2,3],[3,1]]
Output: -1
Explanation: No course can be studied because there is a prerequisite cycle.
Constraints
1 <= n <= 50001 <= relations.length <= 5000relations[i].length == 21 <= prevCoursei, nextCoursei <= nprevCoursei != nextCoursei- All the pairs
[prevCoursei, nextCoursei]are unique.
Common Mistakes
- No prerequisites:
n = 3,relations = []→ return1(all in one semester) - Cycle exists:
n = 2,relations = [[1,2],[2,1]]→ return-1 - Linear chain:
n = 4,relations = [[1,2],[2,3],[3,4]]→ return4 - Multiple paths:
n = 3,relations = [[1,3],[2,3]]→ return2 -
Single node:
n = 1,relations = []→ return1 - Not detecting cycles: Forgetting to check for
-1during DFS - Wrong memoization: Not converting
-1to positive value after computation - Wrong base case: Returning
0instead of1for leaf nodes - Not finding max: Only checking one path instead of maximum across all paths
- Index confusion: Using 0-indexed vs 1-indexed nodes
Alternative Approach: Topological Sort (Kahn’s Algorithm)
class Solution:
def minimumSemesters(self, n, relations):
graph = [[] for _ in range(n + 1)]
for relation in relations:
graph[relation[0]].append(relation[1])
visited = [0] * (n + 1)
maxLen = 1
def dfs(node):
if visited[node] != 0:
return visited[node]
visited[node] = -1
maxLen = 1
for endNode in graph[node]:
length = dfs(endNode)
if length == -1:
return -1
maxLen = max(maxLen, length + 1)
visited[node] = maxLen
return maxLen
for node in range(1, n + 1):
length = dfs(node)
if length == -1:
return -1
maxLen = max(maxLen, length)
return maxLen
Time Complexity: O(V + E)
Space Complexity: O(V + E)
Related Problems
- LC 207: Course Schedule - Check if all courses can be completed
- LC 210: Course Schedule II - Return course ordering
- LC 329: Longest Increasing Path in a Matrix - Similar longest path problem
- LC 802: Find Eventual Safe States - Cycle detection in directed graph
Key Takeaways
- Longest Path in DAG: Minimum semesters = length of longest path
- Each node in path must be taken in sequence
- Longest path determines minimum semesters needed
- Cycle Detection: Three-state coloring
0= unvisited-1= visiting (if encountered again, cycle exists)positive= visited and memoized
- Memoization: Avoids recomputing longest path from each node
- Once computed, result is cached in
visited[node] - Significantly improves efficiency
- Once computed, result is cached in
- DFS Order: Process all neighbors before finalizing current node
- Ensures we find longest path through all possible routes
- Base Case: Node with no outgoing edges has length 1
- Can be taken in first semester (if no prerequisites)
References
- LC 1136: Parallel Courses on LeetCode
- LeetCode Discuss — LC 1136: Parallel Courses
- LeetCode Editorial (may require premium)