Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use leetcode to sort the course schedule in golang

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use leetcode to sort the curriculum in golang. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Example 1:

Enter: 2, [[1pc0]]

Output: [0BI 1]

Explanation: there are a total of 2 courses. To learn course 1, you need to complete course 0. Therefore, the correct order of courses is [0pm 1].

Example 2:

Enter: 4, [1], [2], [3], [3], [3]

Output: [0Jing 1je 2jue 3] or [0Jing 2jue 1jue 3]

Explanation: there are a total of 4 courses. To learn course 3, you should first complete course 1 and course 2. And both course 1 and course 2 should be ranked after course 0.

Therefore, a correct order of courses is [0pm 1pd2p3]. Another correct ranking is [0pc2pm 1pd3].

Description:

The prerequisite for input is the graph represented by the edge list, not the adjacency matrix. See the representation of the figure for details.

You can assume that there are no duplicate edges in the input prerequisites.

Tip:

This problem is equivalent to finding out whether a loop exists in a directed graph. If there is a loop, there is no topological sorting, so it is not possible to select all courses for study.

Topological sorting via DFS-A wonderful video tutorial on Coursera (21 minutes) that introduces the basic concepts of topological sorting.

Topological sorting can also be done through BFS.

Ideas for solving the problem:

1. The ranking of courses is the progressive of the previous article, the top ranking of digraphs, using breadth-first search (BFS).

2, first convert the edge list into an inverse adjacency matrix, and record the enrollment of each prefix course

3. Courses with a degree of 0 have no dependence. You can take them first and put them in the queue.

4, fetch nodes from the queue at a time

A, put in the returned data

B, reduce the degree of entry of all adjacent nodes that depend on this node by one (after deleting this node, the dependency of adjacent nodes is reduced)

C, put the modified node with a degree of 0 into the queue

D, loop until the queue is empty

4. If the length of the returned data is equal to the length of the course, it means there is no loop, otherwise there is a loop.

Func findOrder (numCourses int, prerequisites [] [] int) [] int {inverse_adj:=make ([] [] int,numCourses) out_degree:=make ([] int,numCourses) / / the degree of for iRank / []

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report