-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207_Course Schedule.cpp
More file actions
63 lines (57 loc) · 2.25 KB
/
207_Course Schedule.cpp
File metadata and controls
63 lines (57 loc) · 2.25 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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, is it possible
// for you to finish all courses?
// 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 it is possible.
// 2, [[1,0],[0,1]]
// There are a total of 2 courses to take. To take course 1 you should have finished course 0,
// and to take course 0 you should also have finished course 1. So it is impossible.
// 感想:要用vector而不是hashmap来统计indegree的数量,因为有孤立节点的存在无法遍历到。
// 面试时候要问是否会有duplicate edges的存在。如果有那么out_degree要用hashset来去重。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
// init indegree and outdegree hashmap
vector<int> in_degreee(numCourses); // number of prerequisite courses
unordered_map<int, vector<int> > out_degree; // <prerequisite course, pairs>
for (pair<int, int>& pair : prerequisites) {
++in_degreee[pair.first];
out_degree[pair.second].push_back(pair.first);
}
// init the hashset that contains courses without prerequisites
queue<int> zero_in_degrees;
for (int i = 0; i < numCourses; ++i) {
if (in_degreee[i] == 0) zero_in_degrees.push(i);
}
// take the course from zero_in_degrees
int curr_course;
while (!zero_in_degrees.empty()) {
--numCourses;
curr_course = zero_in_degrees.front(); zero_in_degrees.pop();
for (int next_course : out_degree[curr_course]) {
if (--in_degreee[next_course] == 0) {
zero_in_degrees.push(next_course);
}
}
}
// final result
return numCourses == 0;
}
};
int main() {
vector<pair<int, int> > prerequisites;
prerequisites.push_back(make_pair(1,0));
// prerequisites.push_back(make_pair(0,1));
Solution s;
cout << s.canFinish(2, prerequisites) << endl;
return 0;
}