-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse_schedule.go
56 lines (49 loc) · 1.5 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
package problem0207
/*
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that
you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false
*/
func canFinish(numCourses int, prerequisites [][]int) bool {
var res int
// cur and nxt are for the BFS
var cur, nxt []int
// preCnt[x] shows how many prerequisites x has
var preCnt = make([]int, numCourses)
// depends[x] shows the courses that depend on x
var depends = make([][]int, numCourses)
// Parsing prerequisites list
for _, p := range prerequisites {
depends[p[1]] = append(depends[p[1]], p[0])
preCnt[p[0]]++
}
// Finding all the courses that don't have prerequisites
for i := range preCnt {
if preCnt[i] == 0 {
cur = append(cur, i)
}
}
// BFS over the courses that have no prerequisites
for len(cur) > 0 {
for _, c := range cur {
// Mark the course as done
res++
// Look at all the courses that depend on it
for _, n := range depends[c] {
// Remove the dependencies since we've completed the course
preCnt[n]--
// If it has no additional dependencies
if preCnt[n] == 0 {
// Add it to be completed
nxt = append(nxt, n)
}
}
}
// Switch BFS layers
cur, nxt = nxt, cur
nxt = nxt[:0]
}
return res == numCourses
}