-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
2421-number-of-good-paths.go
108 lines (82 loc) · 1.67 KB
/
2421-number-of-good-paths.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import "sort"
func main() {
}
type UnionFind struct {
parent []int
rank []int
}
func Constructor(n int) UnionFind {
parent := make([]int, n)
rank := make([]int, n)
for i := 0; i < n; i++ {
parent[i] = i
rank[i] = 1
}
return UnionFind{
parent, rank,
}
}
func (u *UnionFind) find(i int) int {
for i != u.parent[i] {
u.parent[i] = u.parent[u.parent[i]]
i = u.parent[i]
}
return i
}
func (u *UnionFind) union(a, b int) bool {
aRoot, bRoot := u.find(a), u.find(b)
if aRoot == bRoot {
return false
}
if u.rank[aRoot] < u.rank[bRoot] {
u.parent[aRoot] = bRoot
u.rank[bRoot] += u.rank[aRoot]
} else {
u.parent[bRoot] = aRoot
u.rank[aRoot] += u.rank[bRoot]
}
return true
}
func getAdjList(edges [][]int) map[int][]int {
adj := make(map[int][]int)
for _, edge := range edges {
a, b := edge[0], edge[1]
adj[a] = append(adj[a], b)
adj[b] = append(adj[b], a)
}
return adj
}
func getValToIndex(vals []int) map[int][]int {
valToIndex := make(map[int][]int)
for i, val := range vals {
valToIndex[val] = append(valToIndex[val], i)
}
return valToIndex
}
func numberOfGoodPaths(vals []int, edges [][]int) int {
adj := getAdjList(edges)
valToIndex := getValToIndex(vals)
res := 0
uf := Constructor(len(vals))
keys := make([]int, 0, len(valToIndex))
for k := range valToIndex {
keys = append(keys, k)
}
sort.Ints(keys)
for _, val := range keys {
for _, i := range valToIndex[val] {
for _, nei := range adj[i] {
if vals[nei] <= vals[i] {
uf.union(nei, i)
}
}
}
count := make(map[int]int)
for _, i := range valToIndex[val] {
count[uf.find(i)] += 1
res += count[uf.find(i)]
}
}
return res
}