Skip to content

Latest commit

 

History

History
82 lines (60 loc) · 2.55 KB

1494-parallel-courses-ii.adoc

File metadata and controls

82 lines (60 loc) · 2.55 KB

1494. Parallel Courses II

{leetcode}/problems/parallel-courses-ii/[LeetCode - 1494. Parallel Courses II ^]

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] = [prevCourse<sub>i</sub>, nextCourse<sub>i</sub>], representing a prerequisite relationship between course prevCourse<sub>i</sub> and course nextCourse<sub>i</sub>: course prevCourse<sub>i</sub> has to be taken before course nextCourse<sub>i</sub>. 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.

Example 1: <img alt="" src="https://assets.leetcode.com/uploads/2020/05/22/leetcode_parallel_courses_1.png" style="width: 269px; height: 147px;" />

Input: n = 4, relations = [[2,1],[3,1],[1,4]], k = 2
Output: 3
Explanation: 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.

Example 2: <img alt="" src="https://assets.leetcode.com/uploads/2020/05/22/leetcode_parallel_courses_2.png" style="width: 271px; height: 211px;" />

Input: n = 5, relations = [[2,1],[3,1],[4,1],[1,5]], k = 2
Output: 4
Explanation: 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.

Constraints:

  • 1 ⇐ n ⇐ 15

  • 1 ⇐ k ⇐ n

  • 0 ⇐ relations.length ⇐ n * (n-1) / 2

  • relations[i].length == 2

  • 1 ⇐ prevCourse<sub>i</sub>, nextCourse<sub>i</sub> ⇐ n

  • prevCourse<sub>i</sub> != nextCourse<sub>i</sub>

  • All the pairs [prevCourse<sub>i</sub>, nextCourse<sub>i</sub>] are unique.

  • The given graph is a directed acyclic graph.

思路分析

一刷
link:{sourcedir}/_1494_ParallelCoursesIi.java[role=include]

参考资料