当前位置:网站首页>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 :
边栏推荐
- Frequent screen flashing after VNC login - abnormal system time
- What is raid? 2000 words can explain RAID 0, 1, 5 and 10 thoroughly, and collect!
- The cloud University of "digital and real integration, CO building authentic Internet" is surging forward
- How to understand EDI requirements of trading partners
- What are the general contents of the enterprise website construction scheme
- Cloud recommendation Vol.1: quick start to remote control under f-stack, go modules and 5g!
- Gin framework: RPC error code design
- Wkwebview audio and video media playback processing
- What is a port? The most complete and strongest port number in history, collection!
- The technical route is based on UE4 for secondary development
猜你喜欢
随机推荐
UNIX command encyclopedia, common commands are here, work must!
How many graphics cards are required for cloud game servers? What should be paid attention to when purchasing servers
The new purchased machines with large customized images are slow to enter the system
Development status of industrial Internet
Initial experience of creating partitioned tables under MySQL cases tdsql for MySQL (I)
Which cloud game service provider is more reliable when the cloud game server is open source
Is a trademark domain name useful? How long does it take to register a domain name?
What is a private VLAN? Eight part essay with pictures and texts.
How to design and make ppt gradient effect?
Opengl: how to use shader to convert RGBA to nv21 image format? (open source for the first time in the whole network)
How to quickly handle third-party login and easy to expand?
What about registered domain names? How long does it take to register a domain name?
How long can the trademark registration be completed? How to improve the speed of trademark registration?
How to build your own cloud game server and what are the building steps
How about Shenzhen website construction? Is it expensive?
Add strikethrough to uilabel
Live broadcast of the double 11 King bombing! Must buy good things introduction, come on~
Simple use of notification
Code 128 barcode details
Compile blender source code


