Skip to content

2359. Find Closest Node to Given Two Nodes #1746

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the closest node in a directed graph that is reachable from two given nodes, node1 and node2, such that the maximum of the distances from node1 and node2 to this node is minimized. If multiple nodes satisfy this condition, we return the smallest index among them. If no such node exists, we return -1.

Approach

  1. Problem Analysis: The graph is represented as a directed graph where each node has at most one outgoing edge. This structure simplifies traversal since each node leads to a unique path (or a cycle). The goal is to find a common node reachable from both node1 and node2 that minimizes the maximum of their distances to this node.
  2. Intuition: For each node, we need the sh…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit May 30, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim May 30, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants