2359. Find Closest Node to Given Two Nodes LeetCode Solution
In this guide, you will get 2359. Find Closest Node to Given Two Nodes LeetCode Solution with the best time and space complexity. The solution to Find Closest Node to Given Two Nodes problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Closest Node to Given Two Nodes solution in C++
- Find Closest Node to Given Two Nodes solution in Java
- Find Closest Node to Given Two Nodes solution in Python
- Additional Resources
Problem Statement of Find Closest Node to Given Two Nodes
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 outgoing edge from i, then edges[i] == -1.
You are also given two integers node1 and node2.
Return the index of the node that can be reached from both node1 and node2, such that the maximum between the distance from node1 to that node, and from node2 to that node is minimized. If there are multiple answers, return the node with the smallest index, and if no possible answer exists, return -1.
Note that edges may contain cycles.
Example 1:
Input: edges = [2,2,3,-1], node1 = 0, node2 = 1
Output: 2
Explanation: The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.
Example 2:
Input: edges = [1,2,-1], node1 = 0, node2 = 2
Output: 2
Explanation: The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.
Constraints:
n == edges.length
2 <= n <= 105
-1 <= edges[i] < n
edges[i] != i
0 <= node1, node2 < n
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2359. Find Closest Node to Given Two Nodes LeetCode Solution in C++
class Solution {
public:
int closestMeetingNode(vector<int>& edges, int node1, int node2) {
constexpr int kMax = 10000;
const vector<int> dist1 = getDist(edges, node1);
const vector<int> dist2 = getDist(edges, node2);
int minDist = kMax;
int ans = -1;
for (int i = 0; i < edges.size(); ++i)
if (min(dist1[i], dist2[i]) >= 0) {
const int maxDist = max(dist1[i], dist2[i]);
if (maxDist < minDist) {
minDist = maxDist;
ans = i;
}
}
return ans;
}
private:
vector<int> getDist(const vector<int>& edges, int u) {
vector<int> dist(edges.size(), -1);
int d = 0;
while (u != -1 && dist[u] == -1) {
dist[u] = d++;
u = edges[u];
}
return dist;
}
};
/* code provided by PROGIEZ */
2359. Find Closest Node to Given Two Nodes LeetCode Solution in Java
class Solution {
public int closestMeetingNode(int[] edges, int node1, int node2) {
final int kMax = 10000;
final int[] dist1 = getDist(edges, node1);
final int[] dist2 = getDist(edges, node2);
int minDist = kMax;
int ans = -1;
for (int i = 0; i < edges.length; ++i)
if (Math.min(dist1[i], dist2[i]) >= 0) {
final int maxDist = Math.max(dist1[i], dist2[i]);
if (maxDist < minDist) {
minDist = maxDist;
ans = i;
}
}
return ans;
}
private int[] getDist(int[] edges, int u) {
int[] dist = new int[edges.length];
Arrays.fill(dist, -1);
int d = 0;
while (u != -1 && dist[u] == -1) {
dist[u] = d++;
u = edges[u];
}
return dist;
}
}
// code provided by PROGIEZ
2359. Find Closest Node to Given Two Nodes LeetCode Solution in Python
class Solution:
def closestMeetingNode(self, edges: list[int], node1: int, node2: int) -> int:
kMax = 10000
dist1 = self._getDist(edges, node1)
dist2 = self._getDist(edges, node2)
minDist = kMax
ans = -1
for i, (d1, d2) in enumerate(zip(dist1, dist2)):
if min(d1, d2) >= 0:
maxDist = max(d1, d2)
if maxDist < minDist:
minDist = maxDist
ans = i
return ans
def _getDist(self, edges: list[int], u: int) -> list[int]:
dist = [-1] * len(edges)
d = 0
while u != -1 and dist[u] == -1:
dist[u] = d
d += 1
u = edges[u]
return dist
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.