Skip to content

Bug Report for lru-cache #5963

Description

@mancini0

Bug Report for https://neetcode.io/problems/lru-cache

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
//import "container/list"

I have to comment out the import of container/list to avoid "# command-line-arguments
./main.go:1:8: list redeclared in this block
./main.go:18:5: other declaration of list
./main.go:1:8: "container/list" imported and not used"

Full solution, which is accepted once the import is commented out:

import "container/list"

type LRUCache struct {
m map[int]*list.Element
capacity int
l *list.List
}

type Pair struct {
key int
value int
}

func Constructor(capacity int) LRUCache {
m := make(map[int]*list.Element)
l := list.New()
return LRUCache {m,capacity,l}
}

func (this *LRUCache) Get(key int) int {
v, ok := this.m[key]
if !ok {
return -1
}else {
this.l.MoveToBack(v)
return v.Value.(Pair).value
}
}

func (this *LRUCache) Put(key int, value int) {

v, ok := this.m[key]
if !ok {
	if len(this.m) >= this.capacity {
		if f:= this.l.Front(); f!= nil {
			delete(this.m, f.Value.(Pair).key)
			this.l.Remove(f)
		}
	}
	el := this.l.PushBack(Pair{key,value})
	this.m[key]=el
} else {
	v.Value = Pair{key: key, value: value}
	this.l.MoveToBack(v)
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions