-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.go
67 lines (62 loc) · 1.38 KB
/
solution.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
type AllOne struct {
count map[string]int
set map[int]map[string]bool
}
func Constructor() AllOne {
return AllOne{
count: make(map[string]int),
set: make(map[int]map[string]bool),
}
}
func (this *AllOne) Inc(key string) {
n := this.count[key]
this.count[key]++
if this.set[n] != nil {
delete(this.set[n], key)
if len(this.set[n]) == 0 {
delete(this.set, n)
}
}
if this.set[n+1] == nil {
this.set[n+1] = make(map[string]bool)
}
this.set[n+1][key] = true
}
func (this *AllOne) Dec(key string) {
n := this.count[key]
this.count[key]--
delete(this.set[n], key)
if len(this.set[n]) == 0 {
delete(this.set, n)
}
if this.count[key] == 0 {
delete(this.count, key)
} else {
if this.set[n-1] == nil {
this.set[n-1] = make(map[string]bool)
}
this.set[n-1][key] = true
}
}
func (this *AllOne) GetMaxKey() string {
if len(this.set) == 0 {
return ""
}
for n := len(this.set); n > 0; n-- {
for key := range this.set[n] {
return key
}
}
return ""
}
func (this *AllOne) GetMinKey() string {
if len(this.set) == 0 {
return ""
}
for n := 1; n <= len(this.set); n++ {
for key := range this.set[n] {
return key
}
}
return ""
}