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. Also, you are given the integer k.
In one semester, you can take at mostk courses as long as you have taken all the prerequisites in the previous semesters for the courses you are taking.
Return theminimum number of semesters needed to take all courses. The testcases will be generated such that it is possible to take every course.

Input: n =4, relations =[[2,1],[3,1],[1,4]], k =2Output: 3Explanation: The figure above represents the given graph.In the first semester, you can take courses 2 and 3.In the second semester, you can take course 1.In the third semester, you can take course 4.

Input: n =5, relations =[[2,1],[3,1],[4,1],[1,5]], k =2Output: 4Explanation: The figure above represents the given graph.In the first semester, you can only take courses 2 and 3 since you cannot take more than two per semester.In the second semester, you can take course 4.In the third semester, you can take course 1.In the fourth semester, you can take course 5.
Since n is small (≤ 15), we can use bitmask DP to represent the set of courses taken. For each state, we try all subsets of available courses (up to k at a time) that can be taken in the next semester, and recursively compute the minimum number of semesters needed.
classSolution {
funminNumberOfSemesters(n: Int, relations: Array<IntArray>, k: Int): Int {
val pre = IntArray(n)
for (r in relations) pre[r[1]-1] = pre[r[1]-1] or (1 shl (r[0]-1))
val dp = IntArray(1 shl n) { n+1 }
dp[0] = 0for (mask in0 until (1 shl n)) {
var avail = 0for (i in0 until n)
if ((mask and (1 shl i)) ==0&& (mask and pre[i]) == pre[i])
avail = avail or (1 shl i)
var sub = avail
while (sub > 0) {
if (Integer.bitCount(sub) <= k)
dp[mask or sub] = minOf(dp[mask or sub], dp[mask]+1)
sub = (sub-1) and avail
}
}
return dp[(1 shl n)-1]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defminNumberOfSemesters(self, n: int, relations: list[list[int]], k: int) -> int:
pre = [0] * n
for u, v in relations:
pre[v-1] |=1<< (u-1)
dp = [n+1] * (1<<n)
dp[0] =0for mask in range(1<<n):
avail =0for i in range(n):
ifnot (mask & (1<<i)) and (mask & pre[i]) == pre[i]:
avail |=1<<i
sub = avail
while sub:
if bin(sub).count('1') <= k:
dp[mask|sub] = min(dp[mask|sub], dp[mask]+1)
sub = (sub-1)&avail
return dp[(1<<n)-1]
impl Solution {
pubfnmin_number_of_semesters(n: i32, relations: Vec<Vec<i32>>, k: i32) -> i32 {
let n = n asusize;
let k = k asusize;
letmut pre =vec![0; n];
for r in relations.iter() {
pre[r[1] asusize-1] |=1<< (r[0] asusize-1);
}
letmut dp =vec![n+1; 1<<n];
dp[0] =0;
for mask in0..(1<<n) {
letmut avail =0;
for i in0..n {
if (mask & (1<<i)) ==0&& (mask & pre[i]) == pre[i] {
avail |=1<<i;
}
}
letmut sub = avail;
while sub >0 {
if sub.count_ones() asusize<= k {
dp[mask|sub] = dp[mask|sub].min(dp[mask]+1);
}
sub = (sub-1)&avail;
}
}
dp[(1<<n)-1]
}
}
⏰ Time complexity: O(n * 2^n + 2^n * C(n, k)), where n is the number of courses. For each state, we try all subsets of available courses (up to k at a time).