-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseSchedule.cpp
31 lines (31 loc) · 966 Bytes
/
CourseSchedule.cpp
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
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> in_degree(numCourses);
vector<vector<int>> adj(numCourses);
for (vector<int>& prerequisite : prerequisites) {
adj[prerequisite[1]].push_back(prerequisite[0]);
++in_degree[prerequisite[0]];
}
queue<int> q;
int classes_taken = 0;
for (int i = 0; i < numCourses; ++i) {
if (!in_degree[i]) {
q.push(i);
++classes_taken;
}
}
while (!q.empty()) {
int curr = q.front();
q.pop();
for (int next : adj[curr]) {
// No more prerequisites for this class.
if (!--in_degree[next]) {
q.push(next);
++classes_taken;
}
}
}
return (classes_taken == numCourses);
}
};