Skip to content

Commit 253e74e

Browse files
committed
Runtime: 3032 ms (Top 5.02%) | Memory: 139.1 MB (Top 14.52%)
1 parent a37df16 commit 253e74e

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1+
# Runtime: 3032 ms (Top 5.02%) | Memory: 139.1 MB (Top 14.52%)
12
class Solution:
23
def closestMeetingNode(self, edges: List[int], node1: int, node2: int) -> int:
3-
4+
45
res = float("inf")
5-
6+
67
def dfs(node, arr, counter=0):
7-
8-
#making sure we haven't visited the node before (i.e., value in the array != -1)
8+
9+
#making sure we haven't visited the node before (i.e., value in the array != -1)
910
while arr[node]==-1 and node!=-1:
10-
11-
#assigning how many moves it takes to reach node
11+
12+
#assigning how many moves it takes to reach node
1213
arr[node] = counter
1314
next_node = edges[node]
14-
15-
#going through each neighbor if exists and updating the counter
15+
16+
#going through each neighbor if exists and updating the counter
1617
dfs(edges[node], arr, counter+1)
1718

1819
return arr
19-
20-
#find moves to reach nodes from node1
20+
21+
#find moves to reach nodes from node1
2122
n1 = [-1 for i in range(len(edges))]
2223
dfs(node1, n1)
23-
24-
#find moves to reach nodes from node2
24+
25+
#find moves to reach nodes from node2
2526
n2 = [-1 for i in range(len(edges))]
2627
dfs(node2, n2)
27-
28+
2829
answer = -1
29-
30+
3031
for i in range(len(edges)):
31-
32-
#check if the end node is reachable from both starting nodes
32+
33+
#check if the end node is reachable from both starting nodes
3334
if n1[i]!=-1 and n2[i]!=-1:
3435
maximum_distance = max(n1[i], n2[i])
35-
36-
#update the distance and the final answer if relevant
36+
37+
#update the distance and the final answer if relevant
3738
if maximum_distance<res:
3839
res = maximum_distance
3940
answer = i
40-
41+
4142
return answer
42-

0 commit comments

Comments
 (0)