forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0207.go
More file actions
31 lines (29 loc) · 643 Bytes
/
0207.go
File metadata and controls
31 lines (29 loc) · 643 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
28
29
30
31
func canFinish(n int, pre [][]int) bool {
g := make(map[int][]int)
d := make([]int, n)
q := make([]int, 0)
vis := make([]int, n)
for _, v := range pre {
g[v[1]] = append(g[v[1]], v[0])
d[v[0]]++
}
for i := 0; i < n; i++ {
if d[i] == 0 {
q = append(q, i)
vis[i] = 1
}
}
for len(q) > 0 {
cur := q[0]
q = q[1:]
n--
for _, i := range g[cur] {
d[i]--
if vis[i] == 0 && d[i] == 0 {
q = append(q, i)
vis[i] = 1
}
}
}
return n == 0
}