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. ES6 export,import报错

    问题描述: 现有两个文件: profile.js const firstName = 'Michael'; const lastName = 'Jackson'; const year = 2018; ...

  2. Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)

    作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...

  3. Spring Boot 学习随记

    微架构的思想在各大互联网公司越来越普及,特此记录Spring Boot的一些细节问题! 网上spring-boot的教程一堆一堆,就没有必要再详细记录了 1:建议通过Idea 来创建spring-bo ...

  4. python进阶训练

    1.列表,字典,集合解析 from random import randint #列表解析,选出大于0的元素 data=[randint(-10,10)for i in range(10)] resu ...

  5. POJ 3856 deltree(模拟)

    Description You have just run out of disk space and decided to delete some of your directories. Rati ...

  6. js+jquery 常用选择器函数

    一.获取当前标签 JS: this,如下: <button onclick="fun(this)"></button> Jquery,如下: $(" ...

  7. 如何在tracepoint上注册函数

    register_trace_##name宏中 tracepoint_probe_register在这个函数中在同一个cp上可以挂多个处理函数, 查看函数:trace_block_rq_issue中定 ...

  8. thrift的lua

    thrift的lua实现 最近要进行系统升级,后台的数据是根据城市区分的.担心新系统的稳定性及新数据的准确性,计划部分城市采用新接口.接口的入参里没有城市信息,只有经纬度坐标,需要调用一个thrift ...

  9. CSS继承—深入剖析

    CSS的继承是指被包在内部的标签将拥有外部标签的样式性质.继承特性最典型的应用通常发挥在整个网页的样式预设,即整体布局声明.而需要要指定为其它样式的部份设定在个别元素里即可达到效果.这项特性可以给网页 ...

  10. [洛谷P4238]【模板】多项式求逆

    题目大意:多项式求逆 题解:$ A^{-1}(x) = (2 - B(x) * A(x)) \times B(x) \pmod{x^n} $ ($B(x)$ 为$A(x)$在$x^{\lceil \d ...