Graph Theory37 sections · 1633 units
Open in Course

LeetCode 721 Accounts Merge - The Graph

Emails are Edges

Do not think of Accounts as nodes. Think of Emails as nodes!

1.1. Assign every unique email an ID (0,1,2,0, 1, 2, \ldots).

2.2. For each account, union all its emails together.

3.3. Group emails by their leader.

4.4. For each group, sort emails and prepend the account name.

This reframing makes the problem clean. The account name is metadata; the emails define connectivity. Time: O(nα(n))O(n \cdot \alpha(n)) where nn is the total number of emails across all accounts.