Solving Leetcode Interviews in Seconds with AI: Maximize the Number of Target Nodes After Connecting Trees II
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3373" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
There exist two undirected trees with n and m nodes, labeled from [0, n - 1] and [0, m - 1], respectively. You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree. Node u is target to node v if the number of edges on the path from u to v is even. Note that a node is always target to itself. Return an array of n integers answer, where answer[i] is the maximum possible number of nodes that are target to node i of the first tree if you had to connect one node from the first tree to another node in the second tree. Note that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query. Example 1: Input: edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]] Output: [8,7,7,8,8] Explanation: For i = 0, connect node 0 from the first tree to node 0 from the second tree. For i = 1, connect node 1 from the first tree to node 4 from the second tree. For i = 2, connect node 2 from the first tree to node 7 from the second tree. For i = 3, connect node 3 from the first tree to node 0 from the second tree. For i = 4, connect node 4 from the first tree to node 4 from the second tree. Example 2: Input: edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]] Output: [3,6,6,6,6] Explanation: For every i, connect node i of the first tree with any node of the second tree. Constraints: 2 <= n, m <= 105 edges1.length == n - 1 edges2.length == m - 1 edges1[i].length == edges2[i].length == 2 edges1[i] = [ai, bi] 0 <= ai, bi < n edges2[i] = [ui, vi] 0 <= ui, vi < m The input is generated such that edges1 and edges2 represent valid trees.
Explanation
Here's the breakdown of the solution:
- Parity-Based Grouping: Determine the parity (even or odd) of the distance of each node from an arbitrary root in both trees. This creates two groups of nodes in each tree: those at even distances and those at odd distances from the root.
- Counting Even/Odd Nodes: Count the number of nodes in each parity group for both trees.
Calculating Maximum Target Nodes: For each node
iin the first tree, calculate the number of target nodes by connecting it to either a node in the even group or a node in the odd group of the second tree, and choose the connection that yields the larger number of target nodes.Runtime Complexity: O(n + m)
- Storage Complexity: O(n + m)
Code
from collections import defaultdict
def solve():
def count_target_nodes(edges1, edges2):
n = len(edges1) + 1
m = len(edges2) + 1
def get_parity_counts(edges, num_nodes):
graph = defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
parity = [-1] * num_nodes
parity[0] = 0 # Root at node 0
queue = [0]
while queue:
u = queue.pop(0)
for v in graph[u]:
if parity[v] == -1:
parity[v] = 1 - parity[u]
queue.append(v)
even_count = parity.count(0)
odd_count = num_nodes - even_count
return even_count, odd_count, parity
even1, odd1, parity1 = get_parity_counts(edges1, n)
even2, odd2, parity2 = get_parity_counts(edges2, m)
answer = []
for i in range(n):
# Connect node i from the first tree to a node with even parity in the second tree
if parity1[i] == 0: # i is at even distance from root1
count1 = even1 + odd2
else: # i is at odd distance from root1
count1 = odd1 + odd2
# Connect node i from the first tree to a node with odd parity in the second tree
if parity1[i] == 0: # i is at even distance from root1
count2 = even1 + even2
else: # i is at odd distance from root1
count2 = odd1 + even2
answer.append(max(count1, count2))
return answer
# Example Usage (using the provided examples)
edges1_1 = [[0, 1], [0, 2], [2, 3], [2, 4]]
edges2_1 = [[0, 1], [0, 2], [0, 3], [2, 7], [1, 4], [4, 5], [4, 6]]
print(f"Example 1: {count_target_nodes(edges1_1, edges2_1)}") # Output: [8, 7, 7, 8, 8]
edges1_2 = [[0, 1], [0, 2], [0, 3], [0, 4]]
edges2_2 = [[0, 1], [1, 2], [2, 3]]
print(f"Example 2: {count_target_nodes(edges1_2, edges2_2)}") # Output: [3, 6, 6, 6, 6]
solve()