leetcode 460. LFU 缓存

10 篇文章 0 订阅
1 篇文章 0 订阅

请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。

实现 LFUCache 类:

LFUCache(int capacity) - 用数据结构的容量 capacity 初始化对象
int get(int key) - 如果键 key 存在于缓存中,则获取键的值,否则返回 -1 。
void put(int key, int value) - 如果键 key 已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量 capacity 时,则应该在插入新项之前,移除最不经常使用的项。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最久未使用 的键。
为了确定最不常使用的键,可以为缓存中的每个键维护一个 使用计数器 。使用计数最小的键是最久未使用的键。

当一个键首次插入到缓存中时,它的使用计数器被设置为 1 (由于 put 操作)。对缓存中的键执行 get 或 put 操作,使用计数器的值将会递增。

函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。

示例:

输入:
[“LFUCache”, “put”, “put”, “get”, “put”, “get”, “get”, “put”, “get”, “get”, “get”]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
输出:
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]

解释:
// cnt(x) = 键 x 的使用计数
// cache=[] 将显示最后一次使用的顺序(最左边的元素是最近的)
LFUCache lfu = new LFUCache(2);
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
lfu.get(1); // 返回 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3); // 去除键 2 ,因为 cnt(2)=1 ,使用计数最小
// cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2); // 返回 -1(未找到)
lfu.get(3); // 返回 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4); // 去除键 1 ,1 和 3 的 cnt 相同,但 1 最久未使用
// cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1); // 返回 -1(未找到)
lfu.get(3); // 返回 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4); // 返回 4
// cache=[3,4], cnt(4)=2, cnt(3)=3

提示:

1 <= capacity <= 104
0 <= key <= 105
0 <= value <= 109
最多调用 2 * 105 次 get 和 put 方法
题目链接:leetcode 460 lfu cache
思路,本道题和 LRU cache 类似,在 LRU cache 的基础上需要维护一个统计次数到 cache 中元素的一个字典, cache 中的元素仍然是一个双向链表,除此之外,还需要维护一个变量,统计当前cache 中的元素的最小出现次数(从cache 中删除元素时,需要根据这个值来进行删除)

class ListNode:
    ## 双向链表
    def __init__(self, key=0, value=0, cnt=1):
        self.key = key       ### cache 中的 key, key 对应的结果是 ListNode 结构
        self.value = value        
        self.prev = None
        self.next = None
        self.cnt = cnt

class cntNode:
    def __init__(self):
        self.dummy = ListNode()
        self.dummy.next = self.dummy.prev = self.dummy

class LFUCache:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self.dummy = ListNode()
        self.dummy.next = self.dummy.prev = self.dummy
        self.key_to_node = dict()
        self.minCnt = 1   ## 记录 cache 里面所有 key 的 minCnt
        ## cnt_to_node 存放以计数器为 key 的 value
        self.cnt_to_node = collections.defaultdict(cntNode)

    def append(self, key, value, cnt=1):
        ## 新节点插入到 dummy 节点的 next 节点
        dummy = self.cnt_to_node[cnt].dummy
        node1 = ListNode(key, value, cnt)
        node1.next = dummy.next
        dummy.next.prev = node1
        dummy.next = node1
        node1.prev = dummy
        self.key_to_node[key] = node1
        
    def delete(self, key):
        node1 = self.key_to_node[key]
        node1.prev.next = node1.next
        node1.next.prev = node1.prev
        node1.prev = node1.next = None
        dummy = self.cnt_to_node[node1.cnt].dummy
        if dummy.next == dummy:  ## 如果该 dict 为空,删除该 dict
            if self.minCnt == node1.cnt:  ## 如果删的是最小的
                self.minCnt += 1
            del self.cnt_to_node[node1.cnt]
        del self.key_to_node[key]

    def get(self, key: int) -> int:
        if key in self.key_to_node:
            node1 = self.key_to_node[key]
            self.delete(key)
            newCnt = node1.cnt + 1
            self.append(key, node1.value, newCnt)
            return node1.value
        else:
            return -1


    def put(self, key: int, value: int) -> None:
        if key in self.key_to_node:
            node1 = self.key_to_node[key]
            self.delete(key)
            self.append(key, value, node1.cnt+1)
        else:
            if len(self.key_to_node) == self.capacity:
                dummy = self.cnt_to_node[self.minCnt].dummy
                self.delete(dummy.prev.key)
            self.append(key, value, 1)
            self.minCnt = 1


# Your LFUCache object will be instantiated and called as such:
# obj = LFUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值