Map-Reduce Instance
I find the strategy to solving this problem an instance of the famous programming pattern "Map-Reduce", where you use step one to map a sparse subset of potential solutions to your problem, followed by a reduction of that subset into the actual solution. The map in this case calculates the distance from a node to all the other nodes using a queue and BFS. The reduce simply goes to the two calculated distance arrays and retrieves the solution in linear time. Code is down below, cheers, ACC. Find Closest Node to Given Two Nodes - LeetCode 2359. Find Closest Node to Given Two Nodes Medium 147 44 Add to List Share You are given a directed graph of n nodes numbered from 0 to n - 1 , where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n , indicating that there is a directed edge from node i to node edges[i] . If there is no o...