Parallel Courses II
Given n courses with prerequisites and a limit k on courses per semester, find the minimum number of semesters needed to complete all courses.
Courses are labeled 1..n. Each pair relations[i] = [u, v] means course u must be completed in an earlier semester than course v. In one semester you may take at most k courses, and every course you take must already have all of its prerequisites completed. Return the minimum number of semesters required to finish every course.
Examples
Input: [4,[[2,1],[3,1],[1,4]],2]
Output: 3
Input: [1,[],1]
Output: 1
Hints
A state is the set of courses already completed, which fits in a bitmask because `n <= 15`. Build `prereq[i]` as the mask of courses that must finish before course `i`.
From a state, the available courses are those not yet taken whose `prereq` mask is a submask of the state. A semester is any non-empty subset of the available courses with size at most `k`, so a BFS over the `2^n` states, or a DP `dp[mask]` = minimum semesters to complete `mask`, is exact.
Taking the `k` "most independent" or deepest ready courses is only a branch-ordering heuristic, not an optimality proof. Correctness needs exhaustive branching over every legal batch with memoization on the completed mask; the safe pruning bound is `ceil(remaining / limit)`, which is a genuine lower bound because a semester holds at most `limit = min(k, n)` courses.
Parallel Courses II
Given n courses with prerequisites and a limit k on courses per semester, find the minimum number of semesters needed to complete all courses.