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

Leetcode 3534. Path Existence Queries in a Graph II

  • Leetcode 3534. Path Existence Queries in a Graph II
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3534. Path Existence Queries in a Graph II

1. 解题思路

这一题是题目3532. Path Existence Queries in a Graph I的进阶版本。

在题目3532当中,我们只需要判断query当中的两个元素是否属于同一个簇,而这里,我们还需要额外判断其最短通路的长度。

因此,在构造出DSU进行元素相连性的判断之后,我们还需要额外对元素的最短通路进行一下考察。

此时,我们只需要对所有的元素按照其元素值进行有序排列,然后考察从某一个元素 u u u开始依照最大步幅maxDiff进行移动,至少需要几次才能达到目标元素 v v v。这个我们可以通过二分搜索进行优化。

2. 代码实现

给出python代码实现如下:

class DSU:def __init__(self, N):self.root = [i for i in range(N)]def find(self, k):if self.root[k] != k:self.root[k] = self.find(self.root[k])return self.root[k]def union(self, a, b):x = self.find(a)y = self.find(b)if x != y:self.root[y] = xreturnclass Solution:def pathExistenceQueries(self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]]) -> List[int]:nodes = sorted([(x, i) for i, x in enumerate(nums)])dsu = DSU(n)for i in range(n-1):if nodes[i+1][0] - nodes[i][0] <= maxDiff:u, v = nodes[i+1][1], nodes[i][1]dsu.union(u, v)@lru_cache(None)def _query(i, j):if nodes[i][0] >= nodes[j][0]:return 0w = nodes[i][1]nxt= bisect.bisect_right(nodes, (nums[w]+maxDiff, n)) - 1return 1 + _query(nxt, j)def query(u, v):if nums[u] > nums[v]:return query(v, u)if dsu.find(u) != dsu.find(v):return -1elif u == v:return 0elif nums[u] == nums[v]:return 1i, j = bisect.bisect_left(nodes, (nums[u], u)), bisect.bisect_left(nodes, (nums[v], v))return _query(i, j)return [query(u, v) for u, v in queries]

提交代码评测得到:耗时1768ms,占用内存175.2MB。


文章转载自:
http://angledozer.sxnf.com.cn
http://chemise.sxnf.com.cn
http://articulatory.sxnf.com.cn
http://bulldozer.sxnf.com.cn
http://anent.sxnf.com.cn
http://bahuvrihi.sxnf.com.cn
http://bacterioscopy.sxnf.com.cn
http://armangite.sxnf.com.cn
http://beef.sxnf.com.cn
http://acholuria.sxnf.com.cn
http://basketstar.sxnf.com.cn
http://atmolyze.sxnf.com.cn
http://astraphobia.sxnf.com.cn
http://admiring.sxnf.com.cn
http://barbule.sxnf.com.cn
http://chace.sxnf.com.cn
http://bilabiate.sxnf.com.cn
http://anadyomene.sxnf.com.cn
http://antihuman.sxnf.com.cn
http://accessible.sxnf.com.cn
http://adenocarcinoma.sxnf.com.cn
http://auscultate.sxnf.com.cn
http://brugge.sxnf.com.cn
http://antefix.sxnf.com.cn
http://astrologist.sxnf.com.cn
http://antagonise.sxnf.com.cn
http://brain.sxnf.com.cn
http://aeronaval.sxnf.com.cn
http://bedew.sxnf.com.cn
http://ablation.sxnf.com.cn
http://www.dtcms.com/a/163058.html

相关文章:

  • 每日一道leetcode(不会做学习版,多学一题)
  • 【Redis】缓存|缓存的更新策略|内存淘汰策略|缓存预热、缓存穿透、缓存雪崩和缓存击穿
  • chatshare.xyz激活码使用说明和渠道指南!
  • JavaScript 中深拷贝浅拷贝的区别?如何实现一个深拷贝?
  • mybatis传递多个不同类型的参数到mapper xml文件
  • 本地大模型编程实战(28)查询图数据库NEO4J(1)
  • 苍穹外卖心得体会
  • 笔试专题(十二)
  • 【动态导通电阻】 GaN PiN二极管电导调制对动态 RON 的影响
  • PDF编辑器:Foxit PDF Editor Pro 版功能解析
  • Ubuntu如何查看硬盘的使用情况,以及挂载情况。
  • 浏览器自动化工具:Selenium 和 Playwright
  • 什么是全景相机?
  • 机器人--相机
  • 学习海康VisionMaster之线圆测量
  • stm32wb55rg (4) 启用usart串口
  • OpenAI Embedding 和密集检索(如 BERT/DPR)进行语义相似度搜索有什么区别和联系
  • transformer-实现单层Decoder 层
  • 【探寻C++之旅】第十二章:异常
  • RTDETRv2 pytorch训练
  • 部署一个自己的Spring Ai 服务(deepseek/通义千问)
  • kotlin flatMap 变换函数的特点和使用场景
  • 亚远景-ASPICE认证:如何优化软件开发流程?
  • 极客天成受邀参加2050大会,共赴人工智能科技盛宴
  • IDEA新版本Local Changes
  • Java后端开发day39--方法引用
  • 【学习资源】知识图谱与大语言模型融合
  • 机器学习之五:基于解释的学习
  • Java语言使用GLM-4-Voice的交互示例
  • CSS Transition入门指南