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

The original problem is here.

The original code is here.

# My Solution

I solve this problem in C++, as below:

``````/*
*Course Schedule II
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
*/
#include<iostream>
#include<queue>
#include<vector>
#include<stdlib.h>
using namespace std;

class Solution {
public:
queue<int> q;  //save the course whos prerequisites are all finished
vector<int> res;
vector<int> empty;
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
if(numCourses <= 0 ){ //|| !canFinish(numCourses, prerequisites
return res;
}
int len = prerequisites.size();
if(len == 0){
for(int i=0;i<numCourses;i++)
res.push_back(i);
return res;
}
vector<int> preNum(numCourses, 0);
for(int i=0;i<len;i++){
preNum[prerequisites[i].first] ++;
}
for(int i=0;i<numCourses;i++){
if(preNum[i] == 0)
q.push(i);
}
while(!q.empty()){
int oneCourse = q.front();
res.push_back(oneCourse);
q.pop();
for(int i=0;i<len;i++){
int course = prerequisites[i].first;
if(preNum[course] == 0)
continue;
if(prerequisites[i].second == oneCourse){
preNum[course] --;
if(preNum[course] == 0){
q.push(course);
}
}
}
}
if(res.size() != numCourses)
return empty;
return res;
}

};

int main(){
Solution s;
pair<int, int> n1(0,1);
//pair<int, int> n2(2,0);
//pair<int, int> n3(3,1);
//pair<int, int> n4(3,2);

vector<pair<int,int> > vec;
vec.push_back(n1);
//vec.push_back(n2);
//vec.push_back(n3);
//vec.push_back(n4);
vector<int> res = s.findOrder(2, vec);
for(int i=0;i<res.size();i++)
cout<<res[i]<<endl;
system("pause");
return 0;
}
``````