Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1568 from razer96/207-Course-Schedule.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dissesmac authored Dec 22, 2022
2 parents 85918f7 + 40de71a commit 7e09652
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions go/207-Course-Schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
func canFinish(numCourses int, prerequisites [][]int) bool {
graph := make(map[int][]int)

for _, prer := range prerequisites {
graph[prer[1]] = append(graph[prer[1]], prer[0])
}

visitSet := make(map[int]struct{})

var dfs func(course int) bool

dfs = func(course int) bool {
if _, ok := visitSet[course]; ok {
return false
}

if len(graph[course]) == 0 {
return true
}

visitSet[course] = struct{}{}

for _, pre := range graph[course] {
if !dfs(pre) {
return false
}
}
delete(visitSet, course)

graph[course] = []int{}

return true
}

for i := 0; i < numCourses; i++ {
if !dfs(i) {
return false
}
}

return true
}

0 comments on commit 7e09652

Please sign in to comment.