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. hdu1505City Game(动态规划)

    City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  2. coia阻止事件上浮

    1.阻止事件上浮 选择默认地址li 时 选中整个div使其为默认地址 此时点击编辑按钮也会触发选中默认事件 为事件添加event.stopPropagation();阻止事件上浮 2.js给页面inp ...

  3. Struts2(三.用户登录状态显示及Struts2标签)

    1.编写main.jsp /WebContent/main.jsp 之前用户登录时已把用户存入session <%@ page language="java" content ...

  4. Python 学习笔记之 Numpy 库——数组基础

    1. 初识数组 import numpy as np a = np.arange(15) a = a.reshape(3, 5) print(a.ndim, a.shape, a.dtype, a.s ...

  5. redis基础和通用key操作

    redis是什么? redis开源的,构建于内存的数据结构的nosql数据库.常被用于数据存储,缓存处理和消息处理. redis的优势? 1.极高的读写能力 2.丰富的数据类型 3.原子性操作 4.支 ...

  6. HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  7. php自学笔记2

    php运行原理: 如果请求服务器上的资源是html网页,服务器直接将网页响应给客户端浏览器: 如果请求服务器上的资源是php,服务器先解释执行php,解释为标准的html代码响应给客户端浏览器.php ...

  8. Web-request内置对象在JSP编程中的应用

  9. PS制作圆角透明图片

    方法一:利用“魔术棒橡皮工具”. 1. 点击“圆角矩形工具”,然后选中打开的背景图片,选中即可. 2.然后,按住“ctrl+enter”,所选边框变为虚线框,然后点击“ctrl+shift+i”反选( ...

  10. zTree基本功能[core]

    zTree 是一个依靠jQuery实现的多功能"树插件".优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree v3.0 将核心代码按照功能进行了分割,不需 ...