“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
[B] [C] [D]
You must take numCourses this semester, marked 0 to numcourses-1.
Some prerequisite courses are required before you can take certain courses. Prerequisites Prerequisites Prerequisites for system system system 2005: System system for System System 2005: System system for System System 2005: System system for System System 2005: System system for System System 2005: System system for system System 2005
- For example, advanced placement courses yes
[0, 1]
Express: Want to learn a course0
You need to complete the course first1
。
Would you please judge whether it is possible to complete all the courses? If so, return true; Otherwise, return false.
Example 1:
Input: numCourses = 2, resident = [[1,0]] output: true Before you can study course 1, you need to complete course 0. It's possible.Copy the code
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]] You need to complete Course 0 before taking course 1; And you should also complete course 1 before taking course 0. It's impossible.Copy the code
Tip:
1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
prerequisites[i]
All the courses in theEach other is not the same
Their thinking
In this case, we can abstract the relationship between courses as a graph, each course has a number of elective courses, the degree is several. So not depend on the courses of course into the degree is 0, we should learn the degrees of 0 course, when the lessons learned, need to rely on its course into 1, so that when a course learned all the courses, it’s into the degree becomes 0, this figure is in fact the process of topological sort. If you don’t know anything about topological sorting, you can read my previous do you really know anything about sorting? This article, we will not repeat here. And pairs of courses that have each other’s prerequisites will form loops in the graph that can’t go to zero, so topological sorting can’t handle them. Therefore, we can abstract the course relations in this question into a graph for topological sorting, and the process of topological sorting is the process of learning courses. In this process, the number of lessons learned is recorded, and when the topological sorting is complete, the number of lessons that can be learned is obtained. At this point, determine whether the number is equal to numCourses. If it is equal, all the courses can be learned; otherwise, all the courses cannot be completed.
The demo
Code implementation
var canFinish = function (numCourses, Const nums = Array(numCourses).fill(0), Map = Array(numCourses) for (let I = 0; i < prerequisites.length; i++) { const [a, System = System name = system name = system name = system name = system name = system name = system name = system name = system name Const queue = [] const queue = [] const queue = [] const queue = [] i < numCourses; I ++) {if (nums[I] === 0) queue.push(I)} while (queue.length) {const cur = queue.shift(), List = map[cur]; If (list) {for (let I = 0; if (list) {for (let I = 0; i < list.length; I ++) {nums[list[I]]-- If (nums[list[I]] === 0) queue.push(list[I])}}} return count === numCourses}Copy the code
At this point we have completed the Leetcode-207-class schedule
If you have any questions or suggestions, please leave a comment! 👏 🏻 👏 🏻 👏 🏻