[Leetcode] 210. Course Schedule II 解题报告

本文介绍了一种解决课程排序问题的算法实现,通过构建有向图并利用拓扑排序的方法来确定完成所有课程的一种可能顺序。文章提供了一个具体的C++代码示例,用于处理课程及其先修条件,并返回一种有效的课程学习顺序。

题目

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

思路

思路完全等同于Leetcode 207,唯一的区别在于需要记录最终的拓扑排序结果。请参考我在《[Leetcode] 207. Course Schedule 解题报告》中的解释和下面的代码片段。

代码

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<unordered_set<int>> graph(numCourses, unordered_set<int>());  
        vector<int> indegree(numCourses, 0);  
        for(auto pre : prerequisites) {                     // initialize the graph  
            if(graph[pre.second].count(pre.first) == 0) {  
                graph[pre.second].insert(pre.first);  
                indegree[pre.first]++;  
            }  
        } 
        vector<int> orders;
        for(int i = 0, j = 0; i < numCourses; i++) {  
            for(j = 0; j < numCourses; j++) {               // find the course that can be taken now  
                if(indegree[j] == 0) {  
                    break;  
                }  
            }  
            if(j == numCourses) {   
                return {};  
            }  
            orders.push_back(j);
            --indegree[j];                                  // update the indegree related to j  
            for(auto p : graph[j]) {  
                --indegree[p];  
            }  
        }  
        return orders;  
    }
};

内容概要:本文系统阐述了Python在数据分析与可视化领域的技术实践,涵盖数据分析基础、数据探索方法、可视化技术原理、高级可视化应用及实战案例五大方面。文章首先介绍NumPy和Pandas在数据处理与描述性统计中的核心作用,继而讲解相关性分析、分布分析和分组对比等探索性分析方法。随后深入剖析Matplotlib、Seaborn和Plotly三大可视化库的技术特点与应用场景,涵盖静态图表、统计图形到交互式可视化。最后通过交通数据的实战案例,演示从数据预处理、探索分析到多维度可视化呈现的完整流程。; 适合人群:具备Python基础、对数据处理与可视化感兴趣的初中级开发者,以及从事数据分析、运营分析、数据科学研究等相关工作的人员;尤其适合工作1-3年、希望提升数据实战能力的研发人员。; 使用场景及目标:①掌握Pandas进行数据清洗、分组聚合与描述性统计的方法;②熟练运用Matplotlib、Seaborn和Plotly实现多样化数据可视化;③通过真实案例理解探索性数据分析流程并构建交互式仪表盘;④应用于业务报表开发、数据洞察挖掘和决策支持系统建设。; 阅读建议:建议结合代码实践同步学习,重点理解不同可视化工具的适用边界,并在实战中尝试迁移应用文中案例逻辑,强化对数据分布识别、多维分析和交互设计的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值