Skip to content

Commit 6b0fd17

Browse files
committed
Sync LeetCode submission Runtime - 219 ms (51.58%), Memory - 48.7 MB (42.99%)
1 parent 546988c commit 6b0fd17

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
2+
3+
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicating that there is a directed edge from node <code>i</code> to node <code>edges[i]</code>. If there is no outgoing edge from <code>i</code>, then <code>edges[i] == -1</code>.</p>
4+
5+
<p>You are also given two integers <code>node1</code> and <code>node2</code>.</p>
6+
7+
<p>Return <em>the <strong>index</strong> of the node that can be reached from both </em><code>node1</code><em> and </em><code>node2</code><em>, such that the <strong>maximum</strong> between the distance from </em><code>node1</code><em> to that node, and from </em><code>node2</code><em> to that node is <strong>minimized</strong></em>. If there are multiple answers, return the node with the <strong>smallest</strong> index, and if no possible answer exists, return <code>-1</code>.</p>
8+
9+
<p>Note that <code>edges</code> may contain cycles.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png" style="width: 321px; height: 161px;" />
14+
<pre>
15+
<strong>Input:</strong> edges = [2,2,3,-1], node1 = 0, node2 = 1
16+
<strong>Output:</strong> 2
17+
<strong>Explanation:</strong> The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
18+
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.
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-4.png" style="width: 195px; height: 161px;" />
23+
<pre>
24+
<strong>Input:</strong> edges = [1,2,-1], node1 = 0, node2 = 2
25+
<strong>Output:</strong> 2
26+
<strong>Explanation:</strong> The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
27+
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.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>n == edges.length</code></li>
35+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
36+
<li><code>-1 &lt;= edges[i] &lt; n</code></li>
37+
<li><code>edges[i] != i</code></li>
38+
<li><code>0 &lt;= node1, node2 &lt; n</code></li>
39+
</ul>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Approach 2: Depth First Search
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def dfs(self, node, edges, dist, visit):
8+
visit[node] = True
9+
neighbor = edges[node]
10+
if neighbor != -1 and not visit[neighbor]:
11+
dist[neighbor] = dist[node] + 1
12+
self.dfs(neighbor, edges, dist, visit)
13+
14+
15+
def closestMeetingNode(self, edges: List[int], node1: int, node2: int) -> int:
16+
n = len(edges)
17+
dist1 = [float('inf')] * n
18+
dist2 = [float('inf')] * n
19+
dist1[node1] = 0
20+
dist2[node2] = 0
21+
22+
visit1 = [False] * n
23+
visit2 = [False] * n
24+
25+
self.dfs(node1, edges, dist1, visit1)
26+
self.dfs(node2, edges, dist2, visit2)
27+
28+
minDistNode = -1
29+
minDistTillNow = float('inf')
30+
31+
for currNode in range(n):
32+
if minDistTillNow > max(dist1[currNode], dist2[currNode]):
33+
minDistNode = currNode
34+
minDistTillNow = max(dist1[currNode], dist2[currNode])
35+
36+
return minDistNode

0 commit comments

Comments
 (0)