# @param {Integer[]} edges
# @param {Integer} node1
# @param {Integer} node2
# @return {Integer}
def closest_meeting_node(edges, node1, node2)
path_a=[];path_b=[]
vis=[]
until edges[node1]==-1
break if vis[node1]
vis[node1]=1
path_a<<node1
node1=edges[node1]
end
path_a<<node1
vis=[]
until edges[node2]==-1
break if vis[node2]
vis[node2]=1
path_b<<node2
node2=edges[node2]
end
path_b<<node2
path_a.uniq!;path_b.uniq!
p path_a,path_b
path_i=path_a&path_b
p path_i
if path_i==[]
return -1
end
# check actual dist!
argmx=mxdist=1<<128
hash_a={};hash_b={}
t=-1
path_a.each{|v|hash_a[v]=(t+=1)}
t=-1
path_b.each{|v|hash_b[v]=(t+=1)}
path_i.each do|v|
puts "distance max to %d is %d" % [v,[hash_a[v],hash_b[v]].max]
if [hash_a[v],hash_b[v]].max<mxdist
mxdist=[hash_a[v],hash_b[v]].max
argmx=v
elsif [hash_a[v],hash_b[v]].max==mxdist
argmx=[argmx,v].min
end
end
argmx
end
2359. Find Closest Node to Given Two Nod
chromat..(hjroh0315)
2023-01-25 13:03
추천 0
다른 게시글
-
53. Maximum Subarray크아아아앙(rkxszkumymda) | 23.01.25추천 0
-
45. Jump Game II크아아아앙(rkxszkumymda) | 23.01.25추천 0
-
55. Jump Game크아아아앙(rkxszkumymda) | 23.01.25추천 0
-
909. Snake and Ladderschromate00(hjroh0315) | 23.01.24추천 0
-
909. Snakes and Ladders [1]개발뉴비(coderhs) | 23.01.24추천 0
-
909. Snakes and Ladders이벤트ㅇㅅ..(219.251) | 23.01.24추천 0
-
997. Find the Town Judge이벤트ㅇㅅ..(219.251) | 23.01.23추천 0
-
997. Find the Town Judge [1]개발뉴비(coderhs) | 23.01.23추천 0
-
백준 9996번키비(qordi124) | 23.01.23추천 0
-
마갤 입문함 [2]키비(qordi124) | 23.01.22추천 0
댓글 0