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. 不能从const char *转换为LPCWSTR --VS经常碰到

    不能从const char *转换为LPCWSTR 在VC 6.0中编译成功的项目在VS2005 vs2005.vs2008.vs2010中常会出现类型错误. 经常出现的错误是:不能从const ch ...

  2. 【架构】技术-工具-平台-语言&框架

    技术-工具-平台-语言&框架 Techniques | Technology Radar | ThoughtWorks

  3. MAC高效软件必备-落雨

    更新时间:2017年09月19日23:45:29 使用MAC有一年多,最想说的莫过于如何打造一个高效的使用Mac的体验. 1. MAC任务栏管理,窗口切换 1. Mac任务栏管理(类似于Windows ...

  4. 【转】java 线程的几种状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

  5. asp.net判断文件或文件夹是否存在

    在上传文件时经常要判断文件夹是否存在,如果存在就上传文件,否则新建文件夹再上传文件 判断语句为 if (System.IO.Directory.Exists(Server.MapPath(" ...

  6. robot framework-databaselibaray库使用(python)

    公司做项目用到了databaselibaray,刚开始使用时碰到了很多问题,网上也查阅了很多资料终于是可以用了,现在整理记录下来,有需要的同学可随意使用: 另,本文主要是databaselibaray ...

  7. C# FTP常规方法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  8. SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现

    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1.    外部架包依赖引入 外部依赖包引入 ...

  9. 12 个非常实用的 jQuery 代码片段

    jQuery是一个非常流行而且实用的JavaScript前端框架,本文并不是介绍jQuery的特效动画,而是分享一些平时积累的12个jQuery实用代码片段,希望对你有所帮助. 导航菜单背景切换效果 ...

  10. jqGrid遍历所有行及获取某一行数据

    $("#gridTable").find("tbody tr").not(".jqgfirstrow").each(function (i) ...