Documentation ¶
Index ¶
- type Heap
- type LFUCache
- type LFUData
- func (this *LFUData) Add(key, val int)
- func (this *LFUData) Get(key int) int
- func (this *LFUData) HasKey(key int) bool
- func (this *LFUData) Index(key int) int
- func (this LFUData) Len() int
- func (this LFUData) Less(i, j int) bool
- func (this *LFUData) Pop() interface{}
- func (this *LFUData) Push(x interface{})
- func (this *LFUData) Remove()
- func (this *LFUData) Replace(key, val int)
- func (this *LFUData) SetIndex(key, idx int)
- func (this LFUData) Swap(i, j int)
- type LFUItem
- type ListNode
- type PriorityQueue
- type TreeNode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LFUCache ¶
type LFUCache struct {
// contains filtered or unexported fields
}
func Constructor ¶
type LFUItem ¶
type LFUItem struct {
// contains filtered or unexported fields
}
460. LFU Cache
Design and implement a data structure for a Least Frequently Used (LFU) cache. Implement the LFUCache class:
- LFUCache(int capacity) Initializes the object with the capacity of the data structure.
- int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
- void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated.
To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key. When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it. The functions get and put must each run in O(1) average time complexity.
Runtime: 682 ms, faster than 90.00% of Go online submissions for LFU Cache. Memory Usage: 85.9 MB, less than 42.50% of Go online submissions for LFU Cache.
func (*LFUItem) SetVersion ¶
type PriorityQueue ¶
type PriorityQueue []*ListNode
func (PriorityQueue) Len ¶
func (q PriorityQueue) Len() int
func (PriorityQueue) Less ¶
func (q PriorityQueue) Less(i, j int) bool
func (*PriorityQueue) Pop ¶
func (q *PriorityQueue) Pop() interface{}
func (*PriorityQueue) Push ¶
func (q *PriorityQueue) Push(x interface{})
func (PriorityQueue) Swap ¶
func (q PriorityQueue) Swap(i, j int)