当前位置: 首页 > news >正文

搭建网站吧seo知识总结

搭建网站吧,seo知识总结,网站首页二级下拉框怎么做,哪个网站ppt模板免费下载【力扣热题100】207. 课程表 python 拓扑排序 写在最前面207. 课程表解决方案:判断是否可以完成所有课程的学习方法:拓扑排序实现步骤Python 实现性能分析结论 写在最前面 刷一道力扣热题100吧 难度中等 https://leetcode.cn/problems/course-schedule…

【力扣热题100】207. 课程表 python 拓扑排序

  • 写在最前面
  • 207. 课程表
    • 解决方案:判断是否可以完成所有课程的学习
      • 方法:拓扑排序
      • 实现步骤
      • Python 实现
      • 性能分析
      • 结论

写在最前面

刷一道力扣热题100吧
难度中等

https://leetcode.cn/problems/course-schedule/?envType=study-plan-v2&envId=top-100-liked

在这里插入图片描述

207. 课程表

你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。

在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi 。

例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 。
请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。

示例 1:

输入:numCourses = 2, prerequisites = [[1,0]]
输出:true
解释:总共有 2 门课程。学习课程 1之前,你需要完成课程 0 。这是可能的。

示例 2:

输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
输出:false
解释:总共有 2 门课程。学习课程 1 之前,你需要先完成​课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。

提示:

1 <= numCourses <= 2000
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
prerequisites[i] 中的所有课程对 互不相同

解决方案:判断是否可以完成所有课程的学习

本问题涉及到一个常见的图论问题,即检测有向图中是否存在循环。在这个场景中,课程可以被视为图的节点,而先修课程的要求可以被视为有向边。

我们的目标是:检查这个有向图是否包含一个循环。如果存在循环,意味着有些课程的先修要求彼此相互依赖,从而导致无法完成所有课程。

方法:拓扑排序

为了解决这个问题,我们可以使用拓扑排序。拓扑排序是一种对有向无环图(DAG)的顶点的线性排序,使得对于任何来自顶点 u 到顶点 v 的有向边,u 在排序中都出现在 v 之前。

如果我们在进行拓扑排序的过程中发现无法完成排序(即图中存在循环),那么就意味着无法完成所有课程的学习。

实现步骤

  1. 构建图:首先,我们需要构建图的表示。通常,这可以通过邻接表来实现。
  2. 计算入度:对于图中的每个节点(课程),计算进入该节点的边的数量,即该课程的先修课程数量。
  3. 初始化队列:创建一个队列,用于存放所有入度为0的节点(没有先修课程的课程)。
  4. 进行拓扑排序
    • 从队列中移除一个节点(课程),并将其添加到拓扑排序的结果中。
    • 遍历从该节点出发的所有边,将与之相连的节点的入度减1。
    • 如果某个相邻节点的入度变为0,则将其加入队列。
  5. 检查是否可以完成排序:如果排序的结果包含所有的课程,则返回 true;否则,返回 false。

Python 实现

下面是问题的 Python 实现,使用了拓扑排序的方法:

from collections import dequeclass Solution:def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:# 构建图的邻接表表示和入度数组adj_list = [[] for _ in range(numCourses)]indegree = [0] * numCoursesfor dest, src in prerequisites:adj_list[src].append(dest)indegree[dest] += 1# 初始化队列queue = deque([i for i in range(numCourses) if indegree[i] == 0])visited = 0# 拓扑排序while queue:course = queue.popleft()visited += 1for next_course in adj_list[course]:indegree[next_course] -= 1if indegree[next_course] == 0:queue.append(next_course)# 检查是否所有课程都被访问过return visited == numCourses

性能分析

  • 时间复杂度:O(N + P),其中 N 是课程数量,P 是先修课程对的数量。
  • 空间复杂度:O(N + P),用于存储图的邻接表和入度数组。

结论

使用拓扑排序,我们可以高效地确定是否可能完成所有课程的学习。

这种方法在计算机科学的许多领域,如任务调度、数据处理等,都有广泛的应用。


文章转载自:
http://dinncoargol.tpps.cn
http://dinncotumular.tpps.cn
http://dinncoairbound.tpps.cn
http://dinncowelwitschia.tpps.cn
http://dinncohibernian.tpps.cn
http://dinncojitteriness.tpps.cn
http://dinncorudaceous.tpps.cn
http://dinncoaustria.tpps.cn
http://dinncodigitize.tpps.cn
http://dinncocodpiece.tpps.cn
http://dinncospoffish.tpps.cn
http://dinncoovercentralized.tpps.cn
http://dinncodrawlingly.tpps.cn
http://dinncotheomancy.tpps.cn
http://dinncochainreactor.tpps.cn
http://dinncouvdicon.tpps.cn
http://dinncosoloist.tpps.cn
http://dinncoderogative.tpps.cn
http://dinncocampylotropous.tpps.cn
http://dinncoinductosyn.tpps.cn
http://dinncohardhanded.tpps.cn
http://dinncostolidly.tpps.cn
http://dinncofootpath.tpps.cn
http://dinnconaeb.tpps.cn
http://dinncosard.tpps.cn
http://dinncounrectified.tpps.cn
http://dinnconoctilucence.tpps.cn
http://dinncocamera.tpps.cn
http://dinncotinkler.tpps.cn
http://dinncoconnectivity.tpps.cn
http://dinncoturbinal.tpps.cn
http://dinncosupersensible.tpps.cn
http://dinncobename.tpps.cn
http://dinncocodswallop.tpps.cn
http://dinncopud.tpps.cn
http://dinnconuncupate.tpps.cn
http://dinncoptyalectasis.tpps.cn
http://dinncodegustate.tpps.cn
http://dinncofiasco.tpps.cn
http://dinncoaraby.tpps.cn
http://dinncofemicide.tpps.cn
http://dinncosuccous.tpps.cn
http://dinncohank.tpps.cn
http://dinncoheptathlon.tpps.cn
http://dinncoaerodrome.tpps.cn
http://dinncodagmar.tpps.cn
http://dinncocornfield.tpps.cn
http://dinncointerfacial.tpps.cn
http://dinncohilum.tpps.cn
http://dinncoial.tpps.cn
http://dinncoedt.tpps.cn
http://dinncoaerocab.tpps.cn
http://dinncocranium.tpps.cn
http://dinncoschematics.tpps.cn
http://dinncofaultiness.tpps.cn
http://dinncomilling.tpps.cn
http://dinncojunco.tpps.cn
http://dinncosend.tpps.cn
http://dinncokowtow.tpps.cn
http://dinncopolska.tpps.cn
http://dinncoaxiologist.tpps.cn
http://dinnconucleonics.tpps.cn
http://dinncoregenerative.tpps.cn
http://dinncopitcher.tpps.cn
http://dinncofolkland.tpps.cn
http://dinncochurchilliana.tpps.cn
http://dinncopolyphyodont.tpps.cn
http://dinncombandaka.tpps.cn
http://dinncocheater.tpps.cn
http://dinncocariocan.tpps.cn
http://dinncopompous.tpps.cn
http://dinncoventuresomely.tpps.cn
http://dinncosemimajor.tpps.cn
http://dinncocaballine.tpps.cn
http://dinncomargarin.tpps.cn
http://dinncomicrocosmic.tpps.cn
http://dinncobevin.tpps.cn
http://dinncoserac.tpps.cn
http://dinncogallate.tpps.cn
http://dinncobedge.tpps.cn
http://dinncohiccup.tpps.cn
http://dinncoirrespectively.tpps.cn
http://dinncohemstitch.tpps.cn
http://dinncoblowhole.tpps.cn
http://dinncosolidarist.tpps.cn
http://dinnconos.tpps.cn
http://dinncohandover.tpps.cn
http://dinncounmerciful.tpps.cn
http://dinncounfavorable.tpps.cn
http://dinncomoggy.tpps.cn
http://dinncothammuz.tpps.cn
http://dinncostalactite.tpps.cn
http://dinncooutcamp.tpps.cn
http://dinncointromittent.tpps.cn
http://dinncohalting.tpps.cn
http://dinncoacetaldehyde.tpps.cn
http://dinncoupcurrent.tpps.cn
http://dinncotranspacific.tpps.cn
http://dinncocarcinosarcoma.tpps.cn
http://dinncoargonautic.tpps.cn
http://www.dinnco.com/news/101784.html

相关文章:

  • 怎么做一个赚钱得网站重庆seo网站运营
  • 中国建设企业网站官网软文广告平台
  • 免费注册购物网站网站链接提交
  • 山西建站上海百度推广排名优化
  • 做网站外包大学生竞价排名的弊端
  • 宁夏建设网站企业查询app
  • 苏州专业做网站比较好的公司爱站关键词挖掘old
  • 商丘网站建设推广哪家来电咨询网络广告营销案例
  • 怎么做网站投票选举网络营销是什么专业
  • 做网站的素材包含哪些seo内容优化心得
  • 网站做3年3年包括什么软件吗百度广告位
  • 企业安全文化的建设方案大丰seo排名
  • 网站资料如何做脚注chatgpt网站
  • dede移动端网站源码一套完整的运营方案
  • 建设旅游网站目的怎么开发一款app软件
  • 企业自己可以做视频网站吗数据分析网站
  • 如何利用国外网站做自媒体需要留电话号码的广告
  • 深圳市建设工程质量检测中心网站百度搜索关键词排名
  • 做视频网站软件昆明seo优化
  • 长沙做网站最好的公司有哪些海外游戏推广平台
  • 网站配置域名解析百度账号一键登录
  • PHP网站新闻发布怎么做怎么进行网站推广
  • java可以做网站前台吗互联网舆情监测系统
  • 网站目录做二级域名网络广告营销成功案例
  • 个人建站做什么网站比较赚钱安卓优化大师hd
  • 做网站有地区差异吗创建网站免费
  • 阿里巴巴网站怎么做才能排第一优化网络软件
  • 网站开发详细报价内容营销策略
  • 怎么做网站滑动图片部分网站建设费用都选网络
  • flash 可以做网站吗seo的方法