-
Notifications
You must be signed in to change notification settings - Fork 0
/
460.go
183 lines (162 loc) · 3.84 KB
/
460.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package p460
import (
"fmt"
)
/**
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.
Follow up:
Could you do both operations in O(1) time complexity?
*/
type LinkMapNode struct {
val int
prev *LinkMapNode
next *LinkMapNode
}
type DLinkNode struct {
count int
keys map[int]*LinkMapNode
head *LinkMapNode
tail *LinkMapNode
prev *DLinkNode
next *DLinkNode
}
func newNode(count int) *DLinkNode {
h := &LinkMapNode{}
t := &LinkMapNode{}
h.next = t
t.prev = h
return &DLinkNode{
count: count,
keys: make(map[int]*LinkMapNode),
head: h,
tail: t}
}
func (n *DLinkNode) addKey(key int) {
ln := &LinkMapNode{val: key}
ln.next = n.tail
ln.prev = n.tail.prev
n.tail.prev.next = ln
n.tail.prev = ln
n.keys[key] = ln
}
func (n *DLinkNode) removeKey(key int) {
node := n.keys[key]
node.next.prev = node.prev
node.prev.next = node.next
delete(n.keys, key)
}
type LFUCache struct {
cap int
hmNode map[int]*DLinkNode
hmValue map[int]int
head *DLinkNode
tail *DLinkNode
}
func Constructor(capacity int) LFUCache {
h := &DLinkNode{}
t := &DLinkNode{}
h.next = t
t.prev = h
return LFUCache{
cap: capacity,
hmNode: make(map[int]*DLinkNode),
hmValue: make(map[int]int),
head: h,
tail: t,
}
}
func (this *LFUCache) Get(key int) int {
ans := -1
if v, ok := this.hmValue[key]; ok {
this.increaseCount(key)
ans = v
}
return ans
}
func (this *LFUCache) increaseCount(key int) {
node := this.hmNode[key]
node.removeKey(key)
if node.next.count == node.count+1 {
node.next.addKey(key)
} else {
tmp := newNode(node.count + 1)
tmp.addKey(key)
tmp.prev = node
tmp.next = node.next
node.next = tmp
tmp.next.prev = tmp
}
this.hmNode[key] = node.next
if len(node.keys) == 0 {
removeNode(node)
}
}
func removeNode(node *DLinkNode) {
node.prev.next = node.next
node.next.prev = node.prev
}
func (this *LFUCache) removeOld() {
if this.head.next == this.tail {
return
}
old := this.head.next.head.next.val
this.head.next.removeKey(old)
if len(this.head.next.keys) == 0 {
removeNode(this.head.next)
}
delete(this.hmValue, old)
delete(this.hmNode, old)
}
func (this *LFUCache) insertAfterHead(node *DLinkNode) {
node.next = this.head.next
node.prev = this.head
this.head.next.prev = node
this.head.next = node
}
func (this *LFUCache) addAfterHead(key int) {
if this.head.next == this.tail ||
this.head.next.count > 0 {
node := newNode(0)
node.addKey(key)
this.insertAfterHead(node)
} else {
this.head.next.addKey(key)
}
this.hmNode[key] = this.head.next
}
func (this *LFUCache) Put(key int, value int) {
if this.cap == 0 {
return
}
if _, ok := this.hmValue[key]; ok {
this.hmValue[key] = value
} else {
if len(this.hmValue) < this.cap {
this.hmValue[key] = value
} else {
this.removeOld()
this.hmValue[key] = value
}
this.addAfterHead(key)
}
this.increaseCount(key)
}
func (this *LFUCache) printList() {
fmt.Println(this.hmValue)
for iter := this.head.next; iter != this.tail; iter = iter.next {
fmt.Print("count ", iter.count, " ", "keys ")
for cur := iter.head.next; cur != iter.tail; cur = cur.next {
fmt.Print(cur.val, " ")
}
fmt.Print(", ")
}
fmt.Println()
}
/**
* Your LRUCache object will be instantiated and called as such:
* obj := Constructor(capacity);
* param_1 := obj.Get(key);
* obj.Put(key,value);
*/