forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0210-course-schedule-ii.go
41 lines (37 loc) · 1015 Bytes
/
0210-course-schedule-ii.go
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
constCRS=0
constPRE=1
funcfindOrder(numCoursesint, prerequisites [][]int) []int {
prereq:=make([][]int, 0)
fori:=0; i<numCourses; i++ {
prereq=append(prereq, make([]int, 0))
}
for_, edge:=rangeprerequisites {
prereq[edge[CRS]] =append(prereq[edge[CRS]], edge[PRE])
}
output:=make([]int, 0)
visit, cycle:=make([]bool, numCourses), make([]bool, numCourses)
vardfsfunc(int) bool
dfs=func(crsint) bool {
ifcycle[crs] {
returnfalse
} elseifvisit[crs] {
returntrue
}
cycle[crs] =true
for_, pre:=rangeprereq[crs] {
if!dfs(pre) {
returnfalse
}
}
cycle[crs] =false
visit[crs] =true
output=append(output, crs)
returntrue
}
forc:=0; c<numCourses; c++ {
ifdfs(c) ==false {
return []int{}
}
}
returnoutput
}