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 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].

本题与207题思路一样,只是返回值由bool型换为整型数组,思路也没变,耗时568ms。

步骤:

  (1)初始化一个大小为numCourses的数组grap;

  (2)将图中个节点的入度保存到数组中,将数组中第一个入度为0的节点找出,并将该节点在数组中的值设为-1,将数组中所有以该顶点为入度的顶点入度减一,将其push到vector中;

  (3)重复(2)numCourses次,若期间在grap数组中没有找到入度为0的顶点,则返回空;

 1 vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites)
 2 {
 3     int grap[numCourses]={0};
 4     vector<int> order;
 5     for (int i=0;i<prerequisites.size();i++)
 6         grap[prerequisites[i].first]++;
 7     for (int j=numCourses-1;j>=0;--j)
 8     {
 9         int del=-1;
10         for (int k=0;k<numCourses;k++)
11         {
12             if (grap[k]==0)
13             {
14                 del=k;
15                 grap[k]=-1;
16                 order.push_back(k);
17                 break;
18             }
19         }
20         if(del==-1)
21         {
22             order.clear();
23             return order;
24         }
25         for (int i=0;i<prerequisites.size();i++)
26         {
27             if (prerequisites[i].second==del)
28                 grap[prerequisites[i].first]--;
29         }
30     }
31     return order;
32 }
时间: 2024-11-10 01:38:07

210. Course Schedule II (易理解拓扑排序算法)的相关文章

拓扑排序算法

#include<stdio.h>#include<stdlib.h>#define MAXVEX 100 //最大顶点数typedef char VertexType; //顶点typedef int EdgeType; //权值#define UNVISITED -1 //标记未访问#define VISITED 1 //标记未访问#define OK 1#define ERROR 0typedef int Status; typedef struct EdgeNode{ in

拓扑排序算法原理以及完整的C代码实现

拓扑排序定义 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前.通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列.简单的说,由某个集合上的一个偏序得到该集合上的一个全序,这个操作称之为拓扑排序. 基础知识 一个较大的工程往往被划分成许多子工程,我们把这些子工程称作活动(activity).在整个

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

和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到,在for (auto p: prerequistites)中特判了输入中可能出现的平行边或自环. 代码: class Solution { public: vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { // [0, {1, 2, 3}], me

[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 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 l

[email&#160;protected] [210]Course Schedule II

Course Schedule II Total Accepted: 13015 Total Submissions: 64067 Difficulty: Medium 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 co

【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 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

[LeetCode] 210. Course Schedule II Java

题目: 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

hiho一下 第四十八周 拓扑排序&#183;二【拓扑排序的应用 + 静态数组 + 拓扑排序算法的时间优化】

题目1 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当然小Hi和小Ho也参与到了其中.从大家各自了解的情况中,小Hi和小Ho整理得到了以下的信息: 校园网主干是由N个节点(编号1..N)组成,这些节点之间有一些单向的网路连接.若存在一条网路连接(u,v)链接了节点u和节点v,则节点u可以向节点v发送信息,但是节点v不能通过该链接向节点u发送信息. 在刚

【数据结构】拓扑排序算法

对AOV网进行拓扑排序的基本思路是: 从AOV网中选择一个入度为0的顶点输出,然后删去此顶点,并删除以此顶点为尾的弧,继续重复此步骤,直到输出全部顶点或者AOV网中不存在入度为0的顶点为止. AOV网及邻接表数据结构: 代码: #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"