Skip to content

Practice Parallel Courses II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1494
LeetCode Problem

Parallel Courses II

Problem Statement

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 most k courses as long as you have taken all the prerequisites in the previous semesters for the courses you are taking. Return the minimum number of semesters needed to take all courses. The testcases will be generated such that it is possible to take every course.

Source: Parallel Courses II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 4, relations = [[2,1],[3,1],[1,4]], k = 2
Output
3

# Example 2

Input
n = 5, relations = [[2,1],[3,1],[4,1],[1,5]], k = 2
Output
4

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 directed acyclic graph.

Related LeetCode Problems

Practice more problems similar to Parallel Courses II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.