forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0207.js
More file actions
27 lines (25 loc) · 622 Bytes
/
0207.js
File metadata and controls
27 lines (25 loc) · 622 Bytes
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
var canFinish = function(n, pre) {
var g = [], d = new Array(n), q = [], vis = new Array(n);
d.fill(0); vis.fill(0);
for (let it of pre) {
g[it[1]] = g[it[1]] || [];
g[it[1]].push(it[0]);
d[it[0]]++;
}
for (let i = 0; i < n; i++) {
if (d[i] == 0) {
q.push(i);
vis[i] == 1;
}
}
while (q.length > 0) {
let cur = q.shift(); n--;
for (let i of g[cur] || []) {
if (!vis[i] && --d[i] == 0) {
vis[i] = 1;
q.push(i);
}
}
}
return n == 0;
};