Skip to content

Commit

Permalink
feat: solve problem 146
Browse files Browse the repository at this point in the history
  • Loading branch information
orgball2608 committed Jul 30, 2024
1 parent cd393f8 commit 32d0c51
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions medium/146. LRU Cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package medium

type LRUNode struct {
Key, Val int
Next, Prev *LRUNode
}

type LRUCache struct {
hash map[int]*LRUNode
head, tail *LRUNode
cap int
len int
}

func ConstructorLRU(capacity int) LRUCache {
head, tail := &LRUNode{}, &LRUNode{}

head.Next = tail
tail.Prev = head
return LRUCache{
hash: make(map[int]*LRUNode, capacity),
head: head,
tail: tail,
cap: capacity,
len: 0,
}
}

func (c *LRUCache) Get(key int) int {
v, ok := c.hash[key]
if !ok {
return -1
}

c.MoveNodeToTop(v)

return v.Val
}

func (c *LRUCache) MoveNodeToTop(node *LRUNode) {
if node == c.head.Next {
return
}
node.Prev.Next = node.Next
node.Next.Prev = node.Prev

first := c.head.Next
node.Next = first
node.Prev = c.head
c.head.Next = node
first.Prev = node
}

func (c *LRUCache) AddNodeToTop(node *LRUNode) {
node.Next = c.head.Next
node.Prev = c.head
c.head.Next.Prev = node
c.head.Next = node
}

func (c *LRUCache) Put(key int, value int) {
if c.Get(key) != -1 {
c.head.Next.Val = value
return
}

if c.len >= c.cap {
delete(c.hash, c.tail.Prev.Key)
c.tail.Prev = c.tail.Prev.Prev
c.tail.Prev.Next = c.tail
c.len--
}

newNode := &LRUNode{Key: key, Val: value}
c.AddNodeToTop(newNode)
c.hash[key] = newNode
c.len++
}

/**
* 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 32d0c51

Please sign in to comment.