This is leetcode 210
There are a total of n courses you have to take labelled from 0 to n - 1.
Some courses may have prerequisites, for example, if prerequisites[i] = [ai, bi] this means you must take the course bi before the course ai.
Given the total number of courses numCourses and a list of the prerequisite pairs, return the ordering of courses you should take to finish all courses.
If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] Output: [0,2,1,3]
When ever I submit this code below I get the runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h) Would appreciate if someone could help me with this!
class Solution {
public:
vector<int> adj[2002];
bool vis[2002];
vector <int> myvect;
void dfs(int node)
{
if(!vis[node])
{
for(int i = 0; i < (int)adj[node].size(); i++)
{
if(!vis[adj[node][i]])
dfs(vis[adj[node][i]]);
}
myvect.push_back(node);
}
}
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
{
vector<int> adj[2002];
int n = prerequisites.size();
int m = prerequisites[0].size();
if(n == 0 && m == 0)
{
for(int i = 0; i < numCourses; i++){
myvect.push_back(i);
}
return myvect;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
adj[i].push_back(j);
}
for(int i = 0; i < numCourses; i++)
dfs(i);
return myvect;
}
};
vis, so reading from it has undefined behaviour.