Course Schedule II

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:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

click to show more hints.

参考Course Schedule,不断删除出度为0的点,该点即下一个选修的课程。

class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> order;
vector<int> outd(numCourses, );
vector<bool> del(numCourses, false);
unordered_map<int, vector<int> > graph;
// construct reverse neighborhood graph
// graph is to decrease the out-degree of a set of vertices,
// when a certain vertice is deleted
for(int i = ; i < prerequisites.size(); i ++)
{
outd[prerequisites[i].first] ++;
graph[prerequisites[i].second].push_back(prerequisites[i].first);
}
int count = ;
while(count < numCourses)
{
int i;
for(i = ; i < numCourses; i ++)
{
if(outd[i] == && del[i] == false)
break;
}
if(i < numCourses)
{
del[i] = true; // delete
order.push_back(i);
for(int j = ; j < graph[i].size(); j ++)
{// decrease the degree of vertices that links to vertice_i
outd[graph[i][j]] --;
}
count ++;
}
else
{// no vertice with 0-degree
vector<int> noorder;
return noorder;
}
}
return order;
}
};

【LeetCode】210. Course Schedule II的更多相关文章

  1. 【LeetCode】210. Course Schedule II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拓扑排序,BFS 拓扑排序,DFS 参考资料 日期 ...

  2. 【刷题-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 cour ...

  3. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  4. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  6. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  7. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  8. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

随机推荐

  1. 利用blob对象实现粘贴图片

    blob的一个常用应用场景,就是获取剪切板上的数据来进行粘贴的操作.例如通过QQ截图后,需要在网页上进行粘贴操作. 粘贴图片我们需要解决下面几个问题 1.监听用户的粘贴操作 2.获取到剪切板上的数据 ...

  2. jquery validation对隐藏的元素不进行验证

    validation默认不会对Hidden元素进行验证的,但最近使用了thinkcmf开发了一个系统后台,在验证时发现隐藏的元素也进行了验证 刚开始以为是 validation版本问题(当前版本取消了 ...

  3. Cognos11中ActiveReport在移动端的应用

    一.环境准备 1.1:前提准备 需要安装了cognos server11,并且已经配置好了服务端 IBM Cognos 版本:IBM Cognos Analytics 11.0.6 IBM Cogno ...

  4. (转)unity使用line renderer画线

    原文地址:http://www.xuanyusong.com/archives/561 任何一个无规则曲线它都是有若干个线段组成,及时是圆形它也是又若干个线段组成的,也就是说将若干个线段拼接起来就是我 ...

  5. Android通用框架设计与完整电商APP开发系列文章

    作者|傅猿猿 责编|Javen205 有福利 有福利 有福利 鸣谢 感谢@傅猿猿 邀请写此系列文章 Android通用框架设计与完整电商APP开发 课程介绍 [导学视频] [课程详细介绍] 以下是部分 ...

  6. 从头来之【iOS及历史版本特性介绍】

    iOS是apple公司的移动操作系统,在iPhone,iPad,iPod中应用,该名最初为Cisco的网络设备操作系统,后授权于Apple公司使用.下面介绍历史版本的特性. iOS1 最大特性是具有其 ...

  7. java.security.NoSuchAlgorithmException: Cannot find any provider supporting DESede/CBC/PKCS5Padding

    最近在做3DES加密,在本地window下面运行ok的程序,放到linux环境上竟然报错: Java.security.NoSuchAlgorithmException: Cannot find an ...

  8. ArcGIS查找空洞多边形

    现需要用ArcGIS将多边形面层中是"空洞"的要素查找出来. 代码思路 一开始没有思路,于是写了代码,基本流程如下: 1)遍历需要判断的要素(可通过属性筛选): 2)检查某一要素相 ...

  9. 使用Visual Studio Code调试React Native报错

    报错信息: [Error] Error: Unknown error: not all success patterns were matched. It means that "react ...

  10. Android 之 获取地理位置及监听

    第一步.添加权限 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> ...