Accounts Merge
LeetCode 721 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू
Union accounts sharing an email.
मी Accounts Merge (LeetCode 721) असा सोडवला — Union Find / DSU पॅटर्नमधील एक मध्यम प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.
खालील विवरण इंग्रजीत आहे.
Intuition
Accounts that share an email belong to the same person. Map each email to the first account that had it; when another account has the same email, union the two accounts. Finally group emails by their account's root.
Approach
- 1
email → first account index; union on repeats.
- 2
Group each email under find(account).
- 3
Output name + sorted emails per group.
मी कसा सोडवला
Disjoint set unionPythonO(M α(N) + M log M)
class DisjointSet:
def __init__(self,n):
self.rank = [0]*(n+1)
self.parents = [i for i in range(n+1)]
def find(self,u):
if self.parents[u]==u:
return u
self.parents[u]=self.find(self.parents[u])
return self.parents[u]
def union(self,u,v):
pu,pv = self.find(u),self.find(v)
if pu==pv:
return
if self.rank[pu]<self.rank[pv]:
self.parents[pu]=pv
elif self.rank[pv]<self.rank[pu]:
self.parents[pv]=pu
elif self.rank[pv]==self.rank[pu]:
self.parents[pv]=pu
self.rank[pu]+=1
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
#account id - info
#email to account id mapping
#if email is already there, connect them using disjoiint set
#merge accounts
#if email's final parent is same, add to the same account - parent account id, email
#final response
#add name, email address
n = len(accounts)
if n==1:
return accounts
ds = DisjointSet(n)
email_account_map = {}
#M emails, N accounts
#M*alpha(N)
for u,account in enumerate(accounts): #M
for email in account[1:]:
if email in email_account_map: #alpha(N)
v = email_account_map[email]
ds.union(u,v)
else:
email_account_map[email]=u
merged_accunts = defaultdict(list)
for email,account in email_account_map.items(): #O(M) - M unique emails
ulp = ds.find(account) #O(alhpa(N))
merged_accunts[ulp].append(email)
res = []
for account,emails in merged_accunts.items():
name = accounts[account][0]
res.append([name]+sorted(emails)) #MlogM
return res
#Space Complexity - O(N) - for DSU
#Email Map - O(M)
#TC - O(M+N)
Accounts Merge — वारंवार विचारले जाणारे प्रश्न
Accounts Merge मागील intuition काय आहे?
Accounts that share an email belong to the same person. Map each email to the first account that had it; when another account has the same email, union the two accounts. Finally group emails by their account's root.
Accounts Merge (LeetCode 721) कसा सोडवावा?
1. email → first account index; union on repeats. 2. Group each email under find(account). 3. Output name + sorted emails per group.
Accounts Merge ची time आणि space complexity किती आहे?
Time: O(M log M). Space: O(M + N). (Accepted approach: Disjoint set union.)
Accounts Merge मध्ये सामान्य चुका कोणत्या?
Names can repeat across different people — only shared emails merge accounts.