当前位置:网站首页>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 :
边栏推荐
- Is the trademark registered domain name legal? How do trademarks register domain names?
- The technical route is based on UE4 for secondary development
- What about registered domain names? How long does it take to register a domain name?
- Development status of industrial Internet
- Data acquisition and transmission instrument environmental pollution monitoring of iron and steel plant
- How to use the cloud game server is the cloud server stable
- Echo framework: add API logging Middleware
- 2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set
- [Tencent cloud double 12.12] from 56 yuan! New users of Tencent cloud buy for the first time, which is more cost-effective!
- Interesting talk about decorator mode, so you will never forget it
猜你喜欢
随机推荐
Use cloudflare to defend against DDoS for free
The United States offered 10million yuan to hunt down blackmail hackers and the energy industry became the "hardest hit" of phishing attacks | global network security hotspot
Start tcapulusdb process
Flink practice tutorial: getting started 1- zero basic users realize simple Flink tasks
How does [lightweight application server] build a cross-border e-commerce management environment?
Initial experience of creating partitioned tables under MySQL cases tdsql for MySQL (I)
Gin framework: add API logging Middleware
Compile blender source code
November 1 global network security hotspot
Buddha's foot before examination: the third bullet of leetcode
NFT metauniverse and the relationship between Games Golden Finance
The easydss on demand file upload interface calls postman to report an error. Failed to upload the file?
Interesting talk about decorator mode, so you will never forget it
Cloud game cannot select a server cloud game server fees
In PHP, use recursive depth to merge multiple arrays
How to build video websites? What are the types of video websites?
Designing complex messaging systems using bridging patterns
Official spoilers! Figure 1 understand Tencent security @2021 Tencent digital ecology Conference
Wkwebview audio and video media playback processing
Afnetworking usage and cache processing


