-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
47 lines (41 loc) · 1.16 KB
/
Copy pathsolution.cpp
File metadata and controls
47 lines (41 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> findOrder(int n, vector<vector<int>> &prerequisites)
{
// adjacency list and indegree array
vector<vector<int>> adj(n);
vector<int> indeg(n, 0);
for (auto &pr : prerequisites)
{
int x = pr[0], y = pr[1]; // to take x, must finish y first -> y -> x
adj[y].push_back(x);
indeg[x]++;
}
// queue of nodes with indegree 0
queue<int> q;
for (int i = 0; i < n; ++i)
if (indeg[i] == 0)
q.push(i);
vector<int> order;
order.reserve(n);
while (!q.empty())
{
int node = q.front();
q.pop();
order.push_back(node);
for (int nei : adj[node])
{
indeg[nei]--;
if (indeg[nei] == 0)
q.push(nei);
}
}
// if all courses included, return order, else return empty
if ((int)order.size() == n)
return order;
return {};
}
};