414. Parallel Courses
You are given n courses labeled from 1 to n, and an array relations where relations[i] = [prevCourse, nextCourse] represents a prerequisite relationship between two courses: you must take prevCourse before nextCourse.
In one semester, you can take any number of courses as long as they do not have prerequisites that have not yet been completed.
Return the minimum number of semesters required to complete all courses. If it is impossible, return -1.
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 they are prerequisites of each other.
Now Your Turn!
Pick the correct output for the given inputInput: n = 4,relations = [[1,2],[1,3],[2,4],[3,4]]
Output: 3
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 104
- 1 <= relations.length <= 104
- relations[i].length == 2
- 1 <= prevCourse, nextCourse <= n
- prevCourse != nextCourse