forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
course-schedule.go
executable file
·63 lines (50 loc) · 1.22 KB
/
course-schedule.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package problem0207
func canFinish(numCourses int, prerequisites [][]int) bool {
// next[i][j] : i -> next[i]... ,i 是 next[i] 的先修课
next := buildNext(numCourses, prerequisites)
// pres[i] : i 的先修课程的**个数**
pres := buildPres(next)
return check(next, pres, numCourses)
}
func buildNext(n int, prereq [][]int) [][]int {
next := make([][]int, n)
for _, pair := range prereq {
next[pair[1]] = append(next[pair[1]], pair[0])
}
return next
}
func buildPres(next [][]int) []int {
pres := make([]int, len(next))
for _, neighbours := range next {
for _, n := range neighbours {
pres[n]++
}
}
return pres
}
func check(next [][]int, pres []int, numCourses int) bool {
var i, j int
// 第 i 个完成的课程的代号是 j
for i = 0; i < numCourses; i++ {
// 完成首先遇到的,无需先修课程的课程
for j = 0; j < numCourses; j++ {
if pres[j] == 0 {
break
}
}
// 每个课程都需要先修课
// 出现了环路
if j >= numCourses {
return false
}
// 修改 pres[j] 为负数
// 避免重修
pres[j] = -1
// 完成 j 课程后
// j 的后续课程的,先修课程数量都可以 -1
for _, c := range next[j] {
pres[c]--
}
}
return true
}