当前位置:网站首页>leetcode-6134: Find the closest node to the given two nodes
leetcode-6134: Find the closest node to the given two nodes
2022-08-01 07:58:00 【Chrysanthemum head bats】
题目
题目连接
给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,每个节点 至多 有一条出边.
Directed graphs use a size of n 下标从 0 开始的数组 edges 表示,表示节点 i There is a directed edge pointing edges[i] .如果节点 i 没有出边,那么 edges[i] == -1 .
Gives you two nodes at the same time node1 和 node2 .
Please return a slave node1 和 node2 can reach the node number,使节点 node1 和节点 node2 distance to this node Larger values are minimized.如果有多个答案,请返回 最小 的节点编号.如果答案不存在,返回 -1 .
注意 edges May contain rings.
示例 1:
输入:edges = [2,2,3,-1], node1 = 0, node2 = 1
输出:2
解释:从节点 0 到节点 2 的距离为 1 ,从节点 1 到节点 2 的距离为 1 .
The larger of the two distances is 1 .We can't get a ratio 1 smaller larger value,So we return to node 2 .
示例 2:
输入:edges = [1,2,-1], node1 = 0, node2 = 2
输出:2
解释:节点 0 到节点 2 的距离为 2 ,节点 2 The distance to itself is 0 .
The larger of the two distances is 2 .We can't get a ratio 2 smaller larger value,So we return to node 2 .
解题
方法一:哈希
创建1a set of hashes,记录从node1Node to start traversing,那么从node2开始遍历,第一次经过setNodes that already exist in ,Then it's the intersection.
But at this time there is no guarantee that it is the minimum distance,So repeat the same thing again,从node2The starting node is recorded toset,然后从node1Begin to meet for the first timesetexisting nodes in .
Take the minimum of the two path results,is the final node.
创建2个数组,visitThe array records whether the node has been visited,lenThe array records the distance from the current node to the starting node
从node1开始,traverse to the end(-1, or a point that has already been visited),同时更新visit和len数组
class Solution {
public:
int closestMeetingNode(vector<int>& edges, int node1, int node2) {
pair<int,int> p1=fun(edges,node1,node2);
pair<int,int> p2=fun(edges,node2,node1);
if(p1.first==p2.first) return p1.first;//如果是同一个节点,Then return directly to the node
else{
if(p1.second==p2.second) return min(p1.first,p2.first);//如果节点不同,But the distance is the same,Select the node with the lower number
else if(p1.second<p2.second) return p1.first;//如果节点不同,and different distances,Then select the node with the smallest distance
else return p2.first;
}
}
pair<int,int> fun(vector<int>& edges, int node1, int node2){
//返回值:(节点,路径)
unordered_set<int> set;//记录node1开始,经过的节点
vector<bool> visit(edges.size(),false);//visitThe array records whether the node has been visited(作用:防止出现环,死循环)
int i=node1;
vector<int> len(edges.size(),-1);//lenThe array records the distances of the nodes passednode1的距离
int curLen=0;
while(true){
if(i==-1||visit[i]==true) break;//If the node has no outgoing edges 或者 节点访问过,那么直接break
visit[i]=true;
len[i]=curLen++;
set.insert(i);
i=edges[i];//访问下一个节点
}
visit=vector<bool>(edges.size(),false);
i=node2;
curLen=0;
while(true){
if(i==-1||visit[i]==true) return {
-1,-1};
visit[i]=true;
if(set.count(i)){
//If the node is fromnode1started visiting,Explain that this is an intersection
int resLen=max(len[i],curLen);//Take the largest path
return {
i,resLen};
}
curLen++;
i=edges[i];
}
return {
-1,-1};
}
};
边栏推荐
猜你喜欢
Golang: go open web service
Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
Chapters 6 and 7 of Huawei Deep Learning Course
HoloView 在 jyputer lab/notebook 不显示总结
What do the values 1, 2, and 3 in nodetype mean?
聊一聊ICMP协议以及ping的过程
最小生成树
leetcode-6133:分组的最大数量
LabVIEW中局部变量和全局变量的分配
随机推荐
The log causes these pits in the thread block, you have to prevent
华为深度学习课程第六、七章
The socket option
Data Analysis 6
JVM内存模型之深究模型特征
pytest接口自动化测试框架 | 单个/多个参数
基于百度OCR的网站验证码在线识别
Shell执行SQL发邮件
插入排序—直接插入排序和希尔排序
13 - JUC CountDownLatch concurrent programming
rhcsa 第三次
好的plm软件有哪些?plm软件排行榜
Golang:go开启web服务
flink sql-client,怎么处理源端与目标增加端,sql-client包括映射表与JOB如
【一句话攻略】彻底理解JS中的回调(Callback)函数
热修复技术可谓是百花齐放
力扣每日一题-第44天-290. 单词规律
JVM:运行时数据区-PC寄存器(程序计数器)
179. 最大数
pytest interface automation testing framework | skip test classes