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.