-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccounts_merge.go
94 lines (84 loc) · 1.95 KB
/
accounts_merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package leetcode
import (
"sort"
)
func accountsMerge(accounts [][]string) [][]string {
uf := UnionFind{make([]int, len(accounts)), make([]int, len(accounts))}
for i := 0; i < len(accounts); i++ {
uf.Roots[i] = i
uf.Ranks[i] = 1
}
mappedAccounts := make([]map[string]bool, len(accounts))
for i, account := range accounts {
m := make(map[string]bool)
for j := 1; j < len(account); j++ {
m[account[j]] = true
}
mappedAccounts[i] = m
}
for i := 0; i < len(mappedAccounts); i++ {
for j := i + 1; j < len(mappedAccounts); j++ {
for key := range mappedAccounts[i] {
if _, ok := mappedAccounts[j][key]; ok == true {
uf.Union(i, j)
}
}
}
}
mergedAccounts := make(map[int]map[string]bool)
for i, m := range mappedAccounts {
root := uf.Find(i)
if _, ok := mergedAccounts[root]; ok == false {
mergedAccounts[root] = make(map[string]bool)
}
val, _ := mergedAccounts[root]
for key := range m {
val[key] = true
}
}
res := make([][]string, len(mergedAccounts))
i := 0
for root, m := range mergedAccounts {
sortedEmails := make([]string, len(m))
j := 0
for key := range m {
sortedEmails[j] = key
j++
}
sort.Strings(sortedEmails)
sortedEmailsWithName := make([]string, len(sortedEmails)+1)
sortedEmailsWithName[0] = accounts[root][0]
for j = 0; j < len(sortedEmails); j++ {
sortedEmailsWithName[j+1] = sortedEmails[j]
}
res[i] = sortedEmailsWithName
i++
}
return res
}
type UnionFind struct {
Roots []int
Ranks []int
}
func (uf UnionFind) Find(x int) int {
for x != uf.Roots[x] {
uf.Roots[x] = uf.Roots[uf.Roots[x]]
x = uf.Roots[x]
}
return x
}
func (uf UnionFind) Union(x, y int) bool {
rootX, rootY := uf.Find(x), uf.Find(y)
if uf.Roots[x] == uf.Roots[y] {
return true
}
if uf.Ranks[rootX] > uf.Ranks[rootY] {
uf.Roots[rootY] = rootX
} else if uf.Ranks[rootY] > uf.Ranks[rootX] {
uf.Roots[rootX] = rootY
} else {
uf.Ranks[rootX] += 1
uf.Roots[rootY] = rootX
}
return false
}