-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
51 lines (48 loc) · 1.04 KB
/
solution1.js
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
/**
* 拓扑排序
*
* 96ms 36.21%
* 42.5mb 11.11%
*
* 时间复杂度 O(n+m)
* 空间复杂度 O(n+m)
*/
const findOrder = function(numCourses, prerequisites) {
const edges = new Map();
const flags = Array(numCourses).fill(0); // 0 未访问 1 当前正在访问 2 访问完成
// 建立邻接表
for (let [next, pre] of prerequisites) {
if (!edges.has(pre)) {
edges.set(pre, []);
}
edges.get(pre).push(next);
}
const stack = [];
for (let i = 0; i < numCourses; i++) {
if (!dfs(edges, flags, i, stack)) {
return [];
}
}
const ans = [];
for (let i = 0; i < numCourses; i++) {
ans[i] = stack.pop();
}
return ans;
function dfs(edgs, flags, index, stack) {
if (flags[index] === 1) {
return false;
}
if (flags[index] === 2) {
return true;
}
flags[index] = 1;
for (let nextIndex of edges.get(index) || []) {
if (!dfs(edgs, flags, nextIndex, stack)) {
return false;
}
}
stack.push(index);
flags[index] = 2;
return true;
}
};