本文涉及的知识点
C++图论 拓扑排序
C++动态规划
LeetCode 2050. 并行课程 III
给你一个整数 n ,表示有 n 节课,课程编号从 1 到 n 。同时给你一个二维整数数组 relations ,其中 relations[j] = [prevCoursej, nextCoursej] ,表示课程 prevCoursej 必须在课程 nextCoursej 之前 完成(先修课的关系)。同时给你一个下标从 0 开始的整数数组 time ,其中 time[i] 表示完成第 (i+1) 门课程需要花费的 月份 数。
请你根据以下规则算出完成所有课程所需要的 最少 月份数:
如果一门课的所有先修课都已经完成,你可以在 任意 时间开始这门课程。
你可以 同时 上 任意门课程 。
请你返回完成所有课程所需要的 最少 月份数。
注意:测试数据保证一定可以完成所有课程(也就是先修课的关系构成一个有向无环图)。
示例 1:
输入:n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
输出:8
解释:上图展示了输入数据所表示的先修关系图,以及完成每门课程需要花费的时间。
你可以在月份 0 同时开始课程 1 和 2 。
课程 1 花费 3 个月,课程 2 花费 2 个月。
所以,最早开始课程 3 的时间是月份 3 ,完成所有课程所需时间为 3 + 5 = 8 个月。
示例 2:
输入:n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
输出:12
解释:上图展示了输入数据所表示的先修关系图,以及完成每门课程需要花费的时间。
你可以在月份 0 同时开始课程 1 ,2 和 3 。
在月份 1,2 和 3 分别完成这三门课程。
课程 4 需在课程 3 之后开始,也就是 3 个月后。课程 4 在 3 + 4 = 7 月完成。
课程 5 需在课程 1,2,3 和 4 之后开始,也就是在 max(1,2,3,7) = 7 月开始。
所以完成所有课程所需的最少时间为 7 + 5 = 12 个月。
提示:
1 <= n <= 5 * 104
0 <= relations.length <= min(n * (n - 1) / 2, 5 * 104)
relations[j].length == 2
1 <= prevCoursej, nextCoursej <= n
prevCoursej != nextCoursej
所有的先修课程对 [prevCoursej, nextCoursej] 都是 互不相同 的。
time.length == n
1 <= time[i] <= 104
先修课程图是一个有向无环图。
动态规划+拓扑排序
动态规划的状态表示
dp[i] 记录学完课程i所需要的最少时间。空间复杂度:O(n)
动态规划的转移方程
dp[i] = times[i] + M a x i 的前置节点 n e x t d p [ n e x t ] Max_{i的前置节点next}dp[next] Maxi的前置节点nextdp[next]
时间复杂度:O(边数)
动态规划的初始值
全部为0。
动态规划的填表顺序
按有向图的拓扑顺序处理,可以保证无后效性。有向图方向:后续课程指向前置课程。建立邻接表的时候,吧从1开始改成从1开始。
动态规划的返回值
dp的最大值
代码
核心代码
class CTopSort
{
public:
template <class T = vector<int> >
void Init(const vector<T>& vNeiBo,bool bDirect )
{
const int iDelOutDeg = bDirect ? 0 : 1;
m_c = vNeiBo.size();
m_vBackNeiBo.resize(m_c);
vector<int> vOutDeg(m_c);
for (int cur = 0; cur < m_c; cur++)
{
vOutDeg[cur] = vNeiBo[cur].size();
for (const auto& next : vNeiBo[cur])
{
m_vBackNeiBo[next].emplace_back(cur);
}
}
vector<bool> m_vHasDo(m_c);
queue<int> que;
for (int i = 0; i < m_c; i++)
{
if ( vOutDeg[i] <= iDelOutDeg)
{
m_vHasDo[i] = true;
if (OnDo(i)) {
que.emplace(i);
}
}
}
while (que.size())
{
const int cur = que.front();
que.pop();
for (const auto& next : m_vBackNeiBo[cur])
{
if (m_vHasDo[next] )
{
continue;
}
vOutDeg[next]--;
if (vOutDeg[next] <= iDelOutDeg )
{
m_vHasDo[next] = true;
if (OnDo(next)) {
que.emplace(next);
}
}
}
};
}
int m_c;
protected:
virtual bool OnDo( int cur) = 0;
vector<vector<int>> m_vBackNeiBo;
};
class CTopSortEx :public CTopSort
{
public:
virtual bool OnDo(int cur) override
{
m_vTopSort.emplace_back(cur);
return true;
}
vector<int> m_vTopSort;
};
class Solution {
public:
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
vector<vector<int>> neiBo(n);
for (const auto& v : relations) {
neiBo[v[1]-1].push_back(v[0]-1);
}
CTopSortEx topSort;
topSort.Init(neiBo, true);
vector<int> dp(n);
for (const auto& cur : topSort.m_vTopSort) {
int iMax = 0;
for (const auto& pre : neiBo[cur]) {
iMax = max(iMax, dp[pre]);
}
dp[cur] = time[cur] + iMax;
}
return *max_element(dp.begin(), dp.end());
}
};
单元测试
int n;
vector<vector<int>> relations;
vector<int> time;
TEST_METHOD(TestMethod11)
{
n = 3, relations = { {1,3},{2,3} }, time = { 3,2,5 };
auto res = Solution().minimumTime(n, relations, time);
AssertEx(8, res);
}
TEST_METHOD(TestMethod12)
{
n = 5, relations = { {1,5},{2,5},{3,5},{3,4},{4,5} }, time = { 1,2,3,4,5 };
auto res = Solution().minimumTime(n, relations, time);
AssertEx(12, res);
}