Skip to content

Commit

Permalink
Time: 124 ms (12.47%), Space: 64 MB (99.06%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Dec 16, 2024
1 parent 8796e2f commit 6765bc5
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions 0146-lru-cache/0146-lru-cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
type Node struct {
Prev, Next *Node
Key, Value int
}

func newNode(key, val int) *Node {
return &Node{
Key: key,
Value: val,
}
}

type LRUCache struct {
Head, Tail *Node
Mp map[int]*Node
Capacity int
}

func newLRUCache(head, tail *Node, cap int) LRUCache {
return LRUCache{
Head: head,
Tail: tail,
Mp: make(map[int]*Node),
Capacity: cap,
}
}

func (this *LRUCache) remove(node *Node) {
delete(this.Mp, node.Key)
node.Prev.Next = node.Next
node.Next.Prev = node.Prev
}

func (this *LRUCache) insert(node *Node) {
this.Mp[node.Key] = node
next := this.Head.Next
this.Head.Next = node
node.Prev = this.Head
next.Prev = node
node.Next = next
}

func Constructor(capacity int) LRUCache {
head, tail := newNode(0, 0), newNode(0, 0)

head.Next = tail
tail.Prev = head
return newLRUCache(head, tail, capacity)
}

func (this *LRUCache) Get(key int) int {
if n, ok := this.Mp[key]; ok {
this.remove(n)
this.insert(n)
return n.Value
}

return -1
}


func (this *LRUCache) Put(key int, value int) {
if _, ok := this.Mp[key]; ok {
this.remove(this.Mp[key])
}

if len(this.Mp) == this.Capacity {
this.remove(this.Tail.Prev)
}

this.insert(newNode(key, value))
}


/**
* Your LRUCache object will be instantiated and called as such:
* obj := Constructor(capacity);
* param_1 := obj.Get(key);
* obj.Put(key,value);
*/

0 comments on commit 6765bc5

Please sign in to comment.