当前位置:网站首页>October 27, 2021: curriculum. You must take numcourses this semester
October 27, 2021: curriculum. You must take numcourses this semester
2022-06-24 02:35:00 【Fuda scaffold constructor's daily question】
2021-10-27: The curriculum . You must take this semester numCourses Courses , Write it down as 0 To numCourses - 1 . Before you take some courses, you need some prerequisite courses . Prerequisite courses by array prerequisites give , among prerequisitesi = ai, bi , If you want to learn a course ai be must Learn the course first bi . for example , The prerequisite course is right for 0, 1 Express : Want to learn the course 0 , You need to finish the course first 1 . Please judge whether it is possible to complete all the courses ? If possible , return true ; otherwise , return false . Power button 207.
Fuda answer 2021-10-27:
A topological sort .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
numCourses := 2
prerequisites := [][]int{{1, 0}}
ret := canFinish1(numCourses, prerequisites)
fmt.Println(ret)
}
// One node, It's just a course
// name It's the course number
// in It's the depth of the course
type Course struct {
name int
in int
nexts []*Course
}
func NewCourse(n int) (res *Course) {
res = &Course{}
res.name = n
res.in = 0
res.nexts = make([]*Course, 0)
return
}
func canFinish1(numCourses int, prerequisites [][]int) bool {
if len(prerequisites) == 0 {
return true
}
// A number Corresponding An example of a class
nodes := make(map[int]*Course)
for _, arr := range prerequisites {
to := arr[0]
from := arr[1] // from -> to
if _, ok := nodes[to]; !ok {
nodes[to] = NewCourse(to)
}
if _, ok := nodes[from]; !ok {
nodes[from] = NewCourse(from)
}
t := nodes[to]
f := nodes[from]
f.nexts = append(f.nexts, t)
t.in++
}
needPrerequisiteNums := len(nodes)
//Queue<Course> zeroInQueue = new LinkedList<>();
zeroInQueue := make([]*Course, 0)
for _, node := range nodes {
if node.in == 0 {
zeroInQueue = append(zeroInQueue, node)
}
}
count := 0
for len(zeroInQueue) > 0 {
//Course cur = zeroInQueue.poll();
cur := zeroInQueue[len(zeroInQueue)-1]
zeroInQueue = zeroInQueue[0 : len(zeroInQueue)-1]
count++
for _, next := range cur.nexts {
next.in--
if next.in == 0 {
zeroInQueue = append(zeroInQueue, next)
}
}
}
return count == needPrerequisiteNums
}The results are as follows :
边栏推荐
- The dealer management and control platform in the leather industry simplifies the purchase approval process and easily controls agents
- Mysql Find_ IN_ Set function
- Vivo global mall: design and practice of commodity system architecture
- A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
- The technical route is based on UE4 for secondary development
- How long can the trademark registration be completed? How to improve the speed of trademark registration?
- Is a trademark domain name useful? How long does it take to register a domain name?
- Live broadcast of the double 11 King bombing! Must buy good things introduction, come on~
- UNIX command encyclopedia, common commands are here, work must!
- Dry goods collection | the most important content collection of Tencent security in the digital ecology Conference
猜你喜欢
随机推荐
Buddha's foot before examination: the second play of leetcode
[Tencent cloud double 12.12] from 56 yuan! New users of Tencent cloud buy for the first time, which is more cost-effective!
Cloud rendering: cloud exhibition hall of Tencent digital ecology Conference - open roaming mode on cloud
Frequent screen flashing after VNC login - abnormal system time
How does easydss solve the problem that the concurrency is too large and the disk read / write cannot keep up?
The difference between classless routing and classless routing
How to build a website? These things should be paid attention to
Advanced BOM tool intelligent packaging function
A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
Mysql Find_ IN_ Set function
Must the company domain name have a trademark registration? What if the registered domain name is rejected?
Start tcapulusdb process
The technical route is based on UE4 for secondary development
The dealer management and control platform in the leather industry simplifies the purchase approval process and easily controls agents
How to use annotations to record operation logs gracefully
Build your own cloud game server. What if the cloud game server is attacked
Build a reliable, scalable and maintainable application system
How to formulate a domain name trademark registration scheme? What if the plan is rejected?
IPhone sending SMS implementation
UNIX command encyclopedia, common commands are here, work must!


