-
Notifications
You must be signed in to change notification settings - Fork 3
/
207.cpp
31 lines (31 loc) · 975 Bytes
/
207.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> inDegrees(numCourses, 0);
vector<vector<int>> graph(numCourses);
for (auto& prerequisite : prerequisites) {
inDegrees[prerequisite[1]]++;
graph[prerequisite[0]].push_back(prerequisite[1]);
}
queue<int> courses;
int count = 0;
for (int i = 0; i < numCourses; ++i) {
if (inDegrees[i] == 0) {
count++;
courses.push(i);
}
}
while (!courses.empty()) {
int course = courses.front();
courses.pop();
for (auto& neighbor : graph[course]) {
inDegrees[neighbor]--;
if (inDegrees[neighbor] == 0) {
courses.push(neighbor);
count++;
}
}
}
return count == numCourses;
}
};