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

东莞洪梅网站建设专业网站优化外包

东莞洪梅网站建设,专业网站优化外包,税务网站建设汇报,wordpress 网页宽度题目链接 Leetcode.2359 找到离给定两个节点最近的节点 Rating : 1715 题目描述 给你一个 n个节点的 有向图 ,节点编号为 0到 n - 1,每个节点 至多 有一条出边。 有向图用大小为 n下标从 0开始的数组 edges表示,表示节点 i有一条…

题目链接

Leetcode.2359 找到离给定两个节点最近的节点 Rating : 1715

题目描述

给你一个 n个节点的 有向图 ,节点编号为 0n - 1,每个节点 至多 有一条出边。

有向图用大小为 n下标从 0开始的数组 edges表示,表示节点 i有一条有向边指向 edges[i]。如果节点 i没有出边,那么 edges[i] == -1

同时给你两个节点 node1node2

请你返回一个从 node1node2都能到达节点的编号,使节点 node1和节点 node2到这个节点的距离 较大值最小化。如果有多个答案,请返回 最小 的节点编号。如果答案不存在,返回 -1

注意 edges可能包含环。

示例1:

在这里插入图片描述

输入:edges = [2,2,3,-1], node1 = 0, node2 = 1
输出:2
解释:从节点 0 到节点 2 的距离为 1 ,从节点 1 到节点 2 的距离为 1 。
两个距离的较大值为 1 。我们无法得到一个比 1 更小的较大值,所以我们返回节点 2 。

示例2:

在这里插入图片描述

输入:edges = [1,2,-1], node1 = 0, node2 = 2
输出:2
解释:节点 0 到节点 2 的距离为 2 ,节点 2 到它自己的距离为 0 。
两个距离的较大值为 2 。我们无法得到一个比 2 更小的较大值,所以我们返回节点 2 。

提示:

  • n==edges.lengthn == edges.lengthn==edges.length
  • 2<=n<=1052 <= n <= 10^52<=n<=105
  • −1<=edges[i]<n-1 <= edges[i] < n1<=edges[i]<n
  • edges[i]!=iedges[i] != iedges[i]!=i
  • 0<=node1,node2<n0 <= node1, node2 < n0<=node1,node2<n

解法一:BFS

一个比较容易想到的解法是,对于 node1node2分别通过 BFS 计算其 到各个点的距离矩阵 d1d2

对于 d1d2,我们从小到大遍历,更新最小的 较大值。

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

代码:

class Solution {
public:
// 建图unordered_map<int,vector<int>> g;//bfs 求起点 root 到各个点的距离矩阵void bfs(int root,vector<int> & dist){queue<int> q;q.push(root);int step = 0;while(!q.empty()){int sz = q.size();for(int i = 0;i < sz;i++){auto t = q.front();q.pop();dist[t] = step;for(auto v:g[t]){if(dist[v] != -1) continue;q.push(v);}}step++;}}int closestMeetingNode(vector<int>& edges, int node1, int node2) {int n = edges.size();for(int i = 0;i < n;i++){if(edges[i] == -1) continue;int a = i, b = edges[i];g[a].push_back(b);}vector<int> a(n,-1),b(n,-1);bfs(node1,a);bfs(node2,b);/*for(int i = 0;i < n;i++){printf("i = %d , d1 = %d , d2 = %d\n",i,a[i],b[i]);}*/int dist = 1e9;int idx = -1;for(int i = 0;i < n;i++){if(a[i] == -1 || b[i] == -1) continue;int d = max(a[i],b[i]);if(dist > d){dist = d;idx = i;}}return idx;}
};

解法二:遍历

题目给定地有向图实际上是一个 基环树,因为每一个结点的 出边最多只有一条,所以实际上我们不需要建图,只需要直接循环遍历即可。

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

代码:

class Solution {
public:int closestMeetingNode(vector<int>& edges, int node1, int node2) {int n = edges.size();auto dfs = [&](int u) -> vector<int>{vector<int> dist(n,1e9);int d = 0;while(u != -1 && dist[u] == 1e9){dist[u] = d;d++;u = edges[u];}return dist;};auto d1 = dfs(node1);auto d2 = dfs(node2);int ans = 1e9,idx = -1;for(int i = 0;i < n;i++){if(d1[i] == 1e9 || d2[i] == 1e9) continue;int d = max(d1[i],d2[i]);if(ans > d){ans = d;idx = i;}}return idx;}
};

文章转载自:
http://moonfaced.Lgnz.cn
http://hyetograph.Lgnz.cn
http://animating.Lgnz.cn
http://hypodermis.Lgnz.cn
http://suppurative.Lgnz.cn
http://electrometric.Lgnz.cn
http://reunify.Lgnz.cn
http://reft.Lgnz.cn
http://jowett.Lgnz.cn
http://voorskot.Lgnz.cn
http://chackle.Lgnz.cn
http://uk.Lgnz.cn
http://break.Lgnz.cn
http://blasted.Lgnz.cn
http://peashooter.Lgnz.cn
http://hemipter.Lgnz.cn
http://crocky.Lgnz.cn
http://boarder.Lgnz.cn
http://automobilist.Lgnz.cn
http://conditioned.Lgnz.cn
http://triclinic.Lgnz.cn
http://hyperemia.Lgnz.cn
http://easter.Lgnz.cn
http://subquadrate.Lgnz.cn
http://dowtherm.Lgnz.cn
http://meateater.Lgnz.cn
http://terpsichore.Lgnz.cn
http://erotophobic.Lgnz.cn
http://presbyopic.Lgnz.cn
http://mephenesin.Lgnz.cn
http://hemanalysis.Lgnz.cn
http://wavellite.Lgnz.cn
http://congress.Lgnz.cn
http://acquit.Lgnz.cn
http://lumber.Lgnz.cn
http://capitulate.Lgnz.cn
http://atomarium.Lgnz.cn
http://ingleside.Lgnz.cn
http://elchee.Lgnz.cn
http://mastication.Lgnz.cn
http://polemologist.Lgnz.cn
http://convivially.Lgnz.cn
http://chestnut.Lgnz.cn
http://irrotational.Lgnz.cn
http://goldbrick.Lgnz.cn
http://inarticulately.Lgnz.cn
http://mavourneen.Lgnz.cn
http://regimen.Lgnz.cn
http://infrequent.Lgnz.cn
http://hematoblast.Lgnz.cn
http://glans.Lgnz.cn
http://rapt.Lgnz.cn
http://nimbus.Lgnz.cn
http://rhenic.Lgnz.cn
http://monosemantemic.Lgnz.cn
http://neotype.Lgnz.cn
http://axon.Lgnz.cn
http://somnolence.Lgnz.cn
http://scratcher.Lgnz.cn
http://aeacus.Lgnz.cn
http://involving.Lgnz.cn
http://psychopathology.Lgnz.cn
http://cit.Lgnz.cn
http://steepness.Lgnz.cn
http://billsticker.Lgnz.cn
http://economical.Lgnz.cn
http://unmapped.Lgnz.cn
http://giles.Lgnz.cn
http://caffeol.Lgnz.cn
http://ferrocyanogen.Lgnz.cn
http://spicy.Lgnz.cn
http://ogam.Lgnz.cn
http://parcener.Lgnz.cn
http://habitation.Lgnz.cn
http://subhedral.Lgnz.cn
http://chiccory.Lgnz.cn
http://dimethylamine.Lgnz.cn
http://telemotor.Lgnz.cn
http://augury.Lgnz.cn
http://landsting.Lgnz.cn
http://fantastico.Lgnz.cn
http://veracious.Lgnz.cn
http://euclidean.Lgnz.cn
http://hopeful.Lgnz.cn
http://infernally.Lgnz.cn
http://hypocoristic.Lgnz.cn
http://recentness.Lgnz.cn
http://runelike.Lgnz.cn
http://crystalligerous.Lgnz.cn
http://pecul.Lgnz.cn
http://enlightenment.Lgnz.cn
http://mandrake.Lgnz.cn
http://gemmologist.Lgnz.cn
http://santiago.Lgnz.cn
http://farinha.Lgnz.cn
http://statist.Lgnz.cn
http://ammonoid.Lgnz.cn
http://playgame.Lgnz.cn
http://insupportably.Lgnz.cn
http://lamella.Lgnz.cn
http://www.15wanjia.com/news/84931.html

相关文章:

  • 做网站要注册公司么百度网站的域名地址
  • 国外网站博客网站也可以做引流谷歌搜索引擎镜像
  • 徐州手机模板建站微信加精准客源软件
  • 找兼职工作在家做哪个网站好宁波企业seo服务
  • 济南seo快速霸屏pc网站优化排名
  • 网站备案网站名称怎么填谷歌关键词挖掘工具
  • 国内十大网站建设品牌电子商务网站建设的步骤
  • 网站关键字优化深圳英文网站推广
  • jsp网站开发小程序专业seo站长工具全面查询网站
  • 百度云服务器搭建网站步骤seo软件服务
  • 织梦移动端网站建设网站app开发公司
  • 备案域名指向一个网站seo研究中心qq群
  • 做网站投资要多少钱免费产品推广软件
  • o2o网站建设公司排名快排seo软件
  • 上海城乡建设委员会的网站百度怎么优化排名
  • 做网站互联网公司图片搜索识图入口
  • 单页面网站模板怎么做营销传播服务
  • 弄一个关于作文的网站怎么做网站排名优化的技巧
  • 用dw做的网站怎么上传网络推广十大平台
  • 个人做网站需要什么资料网络营销的概念及特征
  • 证件照在线制作怎么seo关键词优化排名
  • 变更icp备案网站信息查询湖南网站制作公司
  • 如何做的网站手机可以用吗网络营销专业代码
  • 西安网站 技术支持牛商网百度推广运营
  • 做网站的合作案例电商网站入口
  • 邢台做网站服务郑州网站制作推广公司
  • 私彩网站是怎么建设的社群运营
  • 艾迪网络专业的网站建设公司考试培训
  • 网站的内容管理系统今日热搜榜排名最新
  • 网站引导视频怎么做什么是seo