-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00146-LRU_cache.go
86 lines (72 loc) · 1.66 KB
/
00146-LRU_cache.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
// 146: LRU Cache
// https://leetcode.com/problems/lru-cache/
package main
import "fmt"
// SOLUTION
type Node struct {
k int
v int
previous *Node
next *Node
}
type LRUCache struct {
capacity int
cache map[int]*Node
left *Node
right *Node
}
func Constructor(capacity int) LRUCache {
left, right := &Node{}, &Node{}
left.next, right.previous = right, left
return LRUCache{
capacity: capacity,
cache: map[int]*Node{},
left: left,
right: right,
}
}
func (this *LRUCache) remove(node *Node) {
previous := node.previous;
next := node.next;
previous.next = next;
next.previous = previous;
}
func (this *LRUCache) insert(node *Node) {
previous := this.right.previous
next := this.right
previous.next = node
next.previous = node
node.previous = previous
node.next = next
}
func (this *LRUCache) Get(key int) int {
if _, ok := this.cache[key]; ok {
this.remove(this.cache[key])
this.insert(this.cache[key])
return this.cache[key].v
}
return -1
}
func (this *LRUCache) Put(key int, value int) {
if _, ok := this.cache[key]; ok {this.remove(this.cache[key])}
this.cache[key] = &Node{k: key, v: value}
this.insert(this.cache[key])
if (len(this.cache) > this.capacity) {
lru := this.left.next
this.remove(lru)
delete(this.cache, lru.k)
}
}
func main() {
// OPERATIONS
o := Constructor(2)
o.Put(1, 1)
o.Put(2, 2)
fmt.Println(o.Get(1))
o.Put(3, 3);
fmt.Println(o.Get(2))
o.Put(4, 4);
fmt.Println(o.Get(1))
fmt.Println(o.Get(3))
fmt.Println(o.Get(4))
}