Skip to content

Commit 862db58

Browse files
committed
210 Course Schedule II
1 parent 91e3d47 commit 862db58

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
+ [206 Reverse Linked List(链表转置,递归&迭代)](algorithms/ReverseLinkedList)
117117
+ [207 Course Schedule(拓扑排序、图是否存在环、BFS、DFS)](algorithms/CourseSchedule)
118118
+ [208 Implement Trie (Trie树、字典树、前缀树)](algorithms/ImplementTrie)
119+
+ [210 Course Schedule II(拓扑排序)](algorithms/CourseScheduleII)
119120
+ [211 Add and Search Word - Data structure design(Trie树)](algorithms/AddandSearchWord)
120121

121122
## Database

algorithms/CourseScheduleII/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Course Schedule II
2+
3+
There are a total of n courses you have to take, labeled from 0 to n - 1.
4+
5+
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]
6+
7+
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
8+
9+
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
10+
11+
For example:
12+
13+
```
14+
2, [[1,0]]
15+
```
16+
17+
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is `[0,1]`
18+
19+
```
20+
4, [[1,0],[2,0],[3,1],[3,2]]
21+
```
22+
23+
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is `[0,1,2,3]`. Another correct ordering is`[0,2,1,3]`.
24+
25+
Note:
26+
*The input prerequisites is a graph represented by a list of edges, not adjacency matrices.*
27+
28+
Hints:
29+
30+
* This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
31+
* Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
32+
* Topological sort could also be done via BFS.
33+
34+
## Solution
35+
36+
求拓扑排序结果, 算法见[Course Schedule](../Course Schedule)
37+
38+
```cpp
39+
vector<int> topsort(int n, const vector<pair<int, int>> &request) {
40+
vector<int> result;
41+
vector<int> degree(n, 0);
42+
for (auto p : request) {
43+
degree[p.first]++;
44+
}
45+
while (result.size() < n) { // 还没有访问完
46+
int cur = findZero(degree);
47+
if (cur >= 0) { // 访问节点cur
48+
result.push_back(cur);
49+
degree[cur] = -1; // 标记当前节点为已经访问状态
50+
for (auto p : request) {
51+
if (p.second == cur)
52+
degree[p.first]--; // 去掉已访问节点
53+
}
54+
} else
55+
return vector<int>();
56+
}
57+
return result;
58+
}
59+
```

algorithms/CourseScheduleII/bfs.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <cstdio>
2+
#include <cstdlib>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <utility>
6+
#include <iostream>
7+
using namespace std;
8+
class Solution {
9+
public:
10+
vector<int> findOrder(int n, vector<pair<int, int>> &request) {
11+
return topsort(n, request);
12+
}
13+
private:
14+
/* BFS */
15+
vector<int> topsort(int n, const vector<pair<int, int>> &request) {
16+
vector<int> result;
17+
vector<int> degree(n, 0);
18+
for (auto p : request) {
19+
degree[p.first]++;
20+
}
21+
while (result.size() < n) { // 还没有访问完
22+
int cur = findZero(degree);
23+
if (cur >= 0) { // 访问节点cur
24+
result.push_back(cur);
25+
degree[cur] = -1; // 标记当前节点为已经访问状态
26+
for (auto p : request) {
27+
if (p.second == cur)
28+
degree[p.first]--; // 去掉已访问节点
29+
}
30+
} else
31+
return vector<int>();
32+
}
33+
return result;
34+
}
35+
int findZero(const vector<int> &v) {
36+
int n = v.size();
37+
for (int i = 0; i < n; ++i) {
38+
if (v[i] == 0)
39+
return i;
40+
}
41+
return -1;
42+
}
43+
44+
};
45+
void print(vector<int> v) {
46+
for_each(begin(v), end(v), [](int i){cout << i << ' ';});
47+
cout << endl;
48+
}
49+
int main(int argc, char **argv)
50+
{
51+
Solution solution;
52+
vector<pair<int, int>> request = {make_pair(1, 0), make_pair(2, 0), make_pair(3, 1), make_pair(3, 2)};
53+
print(solution.findOrder(4, request));
54+
return 0;
55+
}

0 commit comments

Comments
 (0)