-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path146-LRU-cache.go
88 lines (79 loc) · 2.03 KB
/
146-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
87
88
// Link: https://leetcode.com/problems/lru-cache/
type Node struct {
Key int
Val int
Next *Node
Prev *Node
}
type LRUCache struct {
cache map[int]*Node
capacity int
head *Node
tail *Node
}
func Constructor(capacity int) LRUCache {
cache := make(map[int]*Node)
return LRUCache{cache, capacity, nil, nil}
}
func (this *LRUCache) handleLRULogic(key int) {
curr := this.cache[key]
prev := curr.Prev
next := curr.Next
if curr == this.head {
this.head = next
}
if prev != nil {
prev.Next = curr.Next
}
next.Prev = prev
curr.Next = nil
this.tail.Next = curr
curr.Prev = this.tail
this.tail = curr
this.cache[key] = curr
}
func (this *LRUCache) Get(key int) int {
if _, ok := this.cache[key]; !ok {
return -1
}
res := this.cache[key].Val
// if not LRU node -> handle LRU logic for middle node
if this.cache[key] != this.tail {
this.handleLRULogic(key)
}
return res
}
func (this *LRUCache) Put(key int, value int) {
if _, ok := this.cache[key]; ok {
if this.cache[key] == this.tail {
this.cache[key].Val = value
return
}
// handle LRU here
this.handleLRULogic(key)
this.cache[key].Val = value
return
}
// If key not exist -> insert at last
size := len(this.cache)
if size == 0 {
this.head = &Node{key, value, nil, nil}
this.tail = this.head
} else if size < this.capacity {
this.tail.Next = &Node{key, value, nil, this.tail}
this.tail = this.tail.Next
} else {
delete(this.cache, this.head.Key)
this.head = this.head.Next
if this.head == nil {
this.head = &Node{key, value, nil, nil}
this.tail = this.head
} else {
this.tail.Next = &Node{key, value, nil, this.tail}
this.tail = this.tail.Next
}
}
this.cache[key] = this.tail
}
// Time complexity for GET and PUT: O(1)
// Space complexity: O(N)