Skip to content

Commit 93dee3b

Browse files
committed
[LeetCode Sync] Runtime - 776 ms (26.85%), Memory - 118 MB (20.14%)
1 parent f15ea63 commit 93dee3b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code> and <code>m - 1</code>, respectively, where <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the first tree and <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the second tree.</p>
2+
3+
<p>You must connect one node from the first tree with another node from the second tree with an edge.</p>
4+
5+
<p>Return the <strong>minimum </strong>possible <strong>diameter </strong>of the resulting tree.</p>
6+
7+
<p>The <strong>diameter</strong> of a tree is the length of the <em>longest</em> path between any two nodes in the tree.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong><img alt="" src="https://assets.leetcode.com/uploads/2024/04/22/example11-transformed.png" /></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<p>We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.</p>
20+
</div>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
<img alt="" src="https://assets.leetcode.com/uploads/2024/04/22/example211.png" />
24+
<div class="example-block">
25+
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]], edges2 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]]</span></p>
26+
27+
<p><strong>Output:</strong> <span class="example-io">5</span></p>
28+
29+
<p><strong>Explanation:</strong></p>
30+
31+
<p>We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.</p>
32+
</div>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= n, m &lt;= 10<sup>5</sup></code></li>
39+
<li><code>edges1.length == n - 1</code></li>
40+
<li><code>edges2.length == m - 1</code></li>
41+
<li><code>edges1[i].length == edges2[i].length == 2</code></li>
42+
<li><code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li>
43+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
44+
<li><code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>
45+
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; m</code></li>
46+
<li>The input is generated such that <code>edges1</code> and <code>edges2</code> represent valid trees.</li>
47+
</ul>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
def minimumDiameterAfterMerge(self, edges1: List[List[int]], edges2: List[List[int]]) -> int:
3+
n = len(edges1) + 1
4+
m = len(edges2) + 1
5+
6+
def build_adj(edges):
7+
adj = defaultdict(list) # creates a dictionary of empty lists
8+
for n1,n2 in edges:
9+
adj[n1].append(n2)
10+
adj[n2].append(n1)
11+
return adj
12+
13+
adj1 = build_adj(edges1)
14+
adj2 = build_adj(edges2)
15+
16+
# DFS : Get diameter
17+
# return: (diam, max_left_path)
18+
def get_diameter(cur, par, adj):
19+
max_d = 0
20+
max_child_paths = [0,0]
21+
22+
for nei in adj[cur]:
23+
if nei == par:
24+
continue
25+
26+
nei_d, nei_max_leaf_path = get_diameter(nei, cur, adj)
27+
max_d = max(max_d, nei_d)
28+
29+
heappush(max_child_paths, nei_max_leaf_path)
30+
heappop(max_child_paths) # min heap gets the minimum and pop it
31+
32+
max_d = max(max_d, sum(max_child_paths))
33+
return [max_d, 1 + max(max_child_paths)]
34+
35+
# diameters
36+
d1, _ = get_diameter(0, -1, adj1)
37+
d2, _ = get_diameter(0, -1, adj2)
38+
39+
return max(d1, d2, ceil(d1/2) + ceil(d2/2) + 1)

0 commit comments

Comments
 (0)