Input:n = 4, relations = [[2,1],[3,1],[1,4]], k = 2
Output:3
Explanation:
Example 2:
Input:n = 5, relations = [[2,1],[3,1],[4,1],[1,5]], k = 2
Output:4
Explanation:
Example 3:
Input:n = 11, relations = [], k = 2
Output:6
Constraints
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 at mostk courses as long as you have completed all prerequisites for the courses in previous semesters.
Return the minimum number of semesters needed to take all courses.
Take courses 2 and 3 in semester 1, course 1 in semester 2, and course 4 in semester 3.
At most two courses can be taken per semester, so course 1 is delayed by prerequisites.
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.