当前位置:网站首页>0DFS Medium LeetCode6134. Find the closest node to the given two nodes
0DFS Medium LeetCode6134. Find the closest node to the given two nodes
2022-08-01 21:35:00 【18 Aru】
6134. Find the closest node to the given two nodes
描述
给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,每个节点 至多 有一条出边.
有向图用大小为 n 下标从 0 开始的数组 edges 表示,表示节点 i 有一条有向边指向 edges[i] .如果节点 i 没有出边,那么 edges[i] == -1 .
同时给你两个节点 node1 和 node2 .
请你返回一个从 node1 和 node2 都能到达节点的编号,使节点 node1 和节点 node2 到这个节点的距离 较大值最小化.如果有多个答案,请返回 最小 的节点编号.如果答案不存在,返回 -1 .
注意 edges 可能包含环.
提示:
n == edges.length
2 <= n <= 105
-1 <= edges[i] < n
edges[i] != i
0 <= node1, node2 < n
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-closest-node-to-given-two-nodes
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
分析
可达性分析
Node1 和node2分别遍历一次,Record the distance to each node,Then iterate over each node,在node1和node2Select from the nodes that can be reachednode1和node2The smallest node that reaches the larger value of this node.
Note the distance from1开始这样node1,node2The distance will not be0,It is convenient to exclude which nodes cannot be reached by two nodes.
class Solution {
public int closestMeetingNode(int[] edges, int node1, int node2) {
int n = edges.length;
int[] len = new int[n];
int[] len2 = new int[n];
int i = node1, j = node2;
int l1 = 1;
int l2 = 1;
while (i != -1 && len[i] == 0) {
len[i] = l1;
l1++;
i = edges[i];
}
int ans = -1;
int max = n+2;
while (j != -1 && len2[j] == 0) {
len2[j] = l2;
l2++;
j = edges[j];
}
for (int k = 0; k < n; k++) {
if (len[k] != 0 && len2[k] != 0) {
int tmp = Math.max(len[k],len2[k]);
if (max > tmp) {
max = tmp;
ans = k;
}
}
}
return ans;
}
}
边栏推荐
- Classification interface, Taobao classification details API
- JSD - 2204 - Knife4j framework - processing - Day07 response results
- C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
- 365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
- 虚拟内存与物理内存之间的关系
- 分类接口,淘宝分类详情 API
- with语句和上下文管理器
- LeetCode
- 牛血清白蛋白-葡聚糖-叶黄素纳米颗粒/半乳糖白蛋白磁性阿霉素纳米粒的制备
- 基于php酒店在线预定管理系统获取(php毕业设计)
猜你喜欢

多商户商城系统功能拆解19讲-平台端发票管理

小程序--分包

FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes

Jmeter实战 | 同用户重复并发多次抢红包

365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号

牛血清白蛋白刺槐豆胶壳聚糖缓释纳米微球/多西紫杉醇的纳米微球DTX-DHA-BSA-NPs

方舟:生存进化官服和私服区别

LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟

RecycleView的使用

ImportError: `save_weights` requires h5py.问题解决
随机推荐
深拷贝浅拷贝
Based on php Xiangxi tourism website management system acquisition (php graduation design)
左旋氧氟沙星/载纳米雄黄磁性/As2O3磁性Fe3O4/三氧化二砷白蛋白纳米球
Upload markdown documents to blog garden
微软校园大使喊你来秋招啦!
Spark shuffle调优
Spark shuffle tuning
分类接口,淘宝分类详情 API
【中文树库标记---CTB】
正则表达式
ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.1 The Prehistoric Phase of the C Language
LeetCode·32.最长有效括号·栈·动态规划
C语言_联合体共用体引入
C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒
一个关于操作数据库的建议—用户密码
Jmeter combat | Repeated and concurrently grabbing red envelopes with the same user
JVM内存结构详解
C Expert Programming Preface