Course Schedule II题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/course-schedule-ii/description/


Description

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

Note:

1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

2. You may assume that there are no duplicate edges in the input prerequisites.

Solution

class Solution {
private:
vector<int> inDegree;
vector<bool> isFinish;
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
inDegree.resize(numCourses);
isFinish.resize(numCourses);
int i, v;
for (i = 0; i < numCourses; i++) {
inDegree[i] = 0;
isFinish[i] = false;
}
for (i = 0; i < prerequisites.size(); i++)
inDegree[prerequisites[i].first]++; queue<int> vq;
for (i = 0; i < numCourses; i++) {
if (inDegree[i] == 0)
vq.push(i);
} vector<int> resultVec;
if (vq.empty())
return resultVec; while (!vq.empty()) {
v = vq.front();
vq.pop();
resultVec.push_back(v);
isFinish[v] = true;
for (i = 0; i < prerequisites.size(); i++) {
if (v == prerequisites[i].second) {
inDegree[prerequisites[i].first]--;
if (inDegree[prerequisites[i].first] == 0 && !isFinish[prerequisites[i].first])
vq.push(prerequisites[i].first);
}
}
} if (resultVec.size() == numCourses) {
return resultVec;
}
else {
vector<int> r;
return r;
}
}
};

解题描述

这道题与Course Schedule的解法基本是一样的。我还是采用了BFS进行拓扑排序,总的来说没有什么新的坑点。

[Leetcode Week4]Course Schedule II的更多相关文章

  1. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  2. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  3. [LeetCode] 210. Course Schedule II 课程清单之二

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  4. Leetcode 210 Course Schedule II

    here are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prere ...

  5. [LeetCode] 210. Course Schedule II 课程安排II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  6. (medium)LeetCode 210.Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. [leetcode]210. Course Schedule II课程表II

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  8. [LeetCode] 207. Course Schedule 课程安排

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. 【LeetCode】210. Course Schedule II

    Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...

随机推荐

  1. 【题解搬运】PAT_A1016 Phone Bills

    从我原来的博客上搬运.原先blog作废. 题目 A long-distance telephone company charges its customers by the following rul ...

  2. Sumsets 递推

    Sumsets Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submi ...

  3. Qt Qpushbutton美化问题

    昨天在论坛看到一个网友的提问,如下 我想到的第一个就是可能需要重载Pushbutton,不过看到有网友回复可以使用stykesheet解决,今天尝试了一下,还是没有成功, 一下是我使用车重载的Push ...

  4. Python 3基础教程26-多行打印

    本文来介绍多行打印.多行打印一般出现在欢迎界面,例如你玩过的游戏,第一个界面,很多文字显示. 我们随便打印几行,来模拟下这种多行打印情况. # 多行打印 print(''' 第一行内容 第二行内容 第 ...

  5. AGV小车典型设计算法及应用

    1. AGV小车的发展背景 在现代化工业的发展中,提倡高效,快速,可靠,提倡将人从简单的工作中解放出来.机器人逐渐替代了人出现在各个工作岗位上.机器人具有可编程.可协调作业和基于传感器控制等特点,自动 ...

  6. mysql 连接问题

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  7. io学习-相关文章

    文章:IO编程 地址:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143 ...

  8. springmvc文件上传,出现400 的错误问题

    遇见的原因是公司系统上的图片上传忽然不好使了,报错400.单独针对这个模块调了好长时间都没解决,后来才发现前几天做过一个excel上传导入的功能... 使用SptingMVC3.1.3 对于文件上传提 ...

  9. Flink History Job

    history job的写入1. org.apache.flink.runtime.jobmanager,Object JobManagerrunJobManager中指定使用MemoryArchiv ...

  10. 【bzoj1391】[Ceoi2008]order 网络流最小割

    原文地址:http://www.cnblogs.com/GXZlegend/p/6796937.html 题目描述 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序 ...