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

代码随想录算法训练营

力扣684.冗余连接【medium】
力扣.冗余连接Ⅱ【hard】

一、力扣684.冗余连接【medium】

题目链接:力扣684.冗余连接
left =x300
视频链接:代码随想录
题解链接:灵茶山艾府

1、思路

  • 可以从前向后遍历每一条边(因为优先让前面的边连上),边的两个节点如果不在同一个集合,就加入集合(即:同一个根节点)。

    • 如图所示,节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。
      • 在这里插入图片描述
    • 如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了
      • 在这里插入图片描述
  • 题目要求返回最后出现的冗余边。当遍历到冗余边时,之前的边已经构建了连通性,此时检测到冗余的边即为最后一条导致环的边。

  • 时间复杂度: O ( n ∗ α ( n ) ) O(n * \alpha (n)) O(nα(n))

2、代码

class Solution:def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:n = len(edges)p = list(range(n + 1))def find(x:int) -> int:if p[x] != x:p[x] = find(p[x])return p[x]def union(i,j):p[find(i)] = find(j)for a, b in edges:if find(a) != find(b):union(a, b)else:return [a,b]return []     

二、力扣.冗余连接Ⅱ【hard】

题目链接:力扣冗余连接Ⅱ
视频链接:代码随想录

1、思路

  • 冗余的边有两种情况:
    • 入度为2的节点:节点有2个父节点
    • 有向环
  • 太复杂了,跳过
  • 时间复杂度: O ( n ) O(n) O(n)

2、代码

class UnionFind:def __init__(self, n):self.ancestor = list(range(n))def union(self, index1: int, index2: int):self.ancestor[self.find(index1)] = self.find(index2)def find(self, index: int) -> int:if self.ancestor[index] != index:self.ancestor[index] = self.find(self.ancestor[index])return self.ancestor[index]class Solution:def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:n = len(edges)uf = UnionFind(n + 1)parent = list(range(n + 1))conflict = -1cycle = -1for i, (node1, node2) in enumerate(edges):if parent[node2] != node2:conflict = ielse:parent[node2] = node1if uf.find(node1) == uf.find(node2):cycle = ielse:uf.union(node1, node2)if conflict < 0:return [edges[cycle][0], edges[cycle][1]]else:conflictEdge = edges[conflict]if cycle >= 0:return [parent[conflictEdge[1]], conflictEdge[1]]else:return [conflictEdge[0], conflictEdge[1]]

三、力扣

题目链接:力扣
left =x300
视频链接:代码随想录
题解链接:灵茶山艾府

1、思路

  • 时间复杂度: O ( n ) O(n) O(n)

2、代码



四、力扣

题目链接:力扣
left =x300
视频链接:代码随想录
题解链接:灵茶山艾府

1、思路

  • 时间复杂度: O ( n ) O(n) O(n)

2、代码



相关文章:

  • Vue响应式系统演进与实现解析
  • 集成思想在算法(目标检测)中的体现
  • Python入门手册:Python简介,什么是Python
  • 操作系统----软考中级软件工程师(自用学习笔记)
  • 使用 GitHub Pages 部署单页面应用教程
  • vue路由小案例
  • Failed to resolve import “echarts“ from “src/views/HistoricalData.vue“.
  • 第 4 章:网络与总线——CAN / Ethernet / USB-OTG
  • 国产视频转换LT6211UX:HDMI2.0转LVDS/MIPI芯片简介,支持4K60Hz
  • File的使用
  • 聚焦开放智能,抢占技术高地 | 2025 高通边缘智能创新应用大赛第五场公开课来袭!
  • React表单开发的瑞士军刀:Formik与Yup实战指南
  • Android7 Input(八)App Input事件接收器InputEventReceiver
  • Visual Studio 2019/2022:当前不会命中断点,还没有为该文档加载任何符号。
  • IP风险画像技术:如何用20+维度数据构建网络安全护城河?
  • Web Scraping vs API:选择正确数据提取方法的终极指南(二)
  • WebSocket心跳机制
  • 深度解析Vue项目Webpack打包分包策略 从基础配置到高级优化,全面掌握性能优化核心技巧
  • hghac集群服务器时间同步(chrony同步)
  • IntentUri页面跳转
  • 做建网站的工作一年赚几百万/系统优化是什么意思
  • wordpress 5.0.2主题/怎么给网站做优化
  • 快速做效果图的网站叫什么/微信最好用的营销软件
  • 西安专业网站建设服务好/新媒体运营是做什么
  • 合肥能做网站的公司/seo精灵
  • 亚马逊deal网站怎么做/推广哪个平台好