目录

0460:LFU 缓存(★★)

力扣第 460 题

题目

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

实现 LFUCache 类:

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

为了确定最不常使用的键,可以为缓存中的每个键维护一个 使用计数器 。使用计数最小的键是最久未使用的键。

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

函数 getput 必须以 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 * 105getput 方法

分析

#1

0146 升级版,同样可以用有序集合解决。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class LFUCache:

    def __init__(self, capacity: int):
        from sortedcontainers import SortedList
        self.sl = SortedList()
        self.m = capacity
        self.t = 0
        self.d = {}

    def get(self, key: int) -> int:
        if key not in self.d:
            return -1
        w,t,x = self.d[key]
        self.sl.remove((w,t,key))
        self.sl.add((w+1,self.t,key))
        self.d[key] = (w+1,self.t,x)
        self.t += 1
        return x

    def put(self, key: int, value: int) -> None:
        if key in self.d:
            w,t,_ = self.d[key]
            self.sl.remove((w,t,key))
            self.sl.add((w+1,self.t,key))
            self.d[key] = (w+1,self.t,value)
        else:
            if len(self.sl)==self.m:
                _,_,x = self.sl.pop(0)
                self.d.pop(x)
            self.sl.add((1,self.t,key))
            self.d[key] = (1,self.t,value)
        self.t += 1

553 ms

#2

要求 O(1),同样考虑用 OrderedDict:

  • 计数器 ct 维护每个 key 的频率
  • 将相同频率的 key 放一起,用 OrderedDict 维护顺序
  • 维护最小频率 minw
    • get 或 put 时,若已有 key,则 minw 不变或加 1
    • put 新 key 时,minw 变为 1
  • 其它模拟即可

解答

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class LFUCache:

    def __init__(self, capacity: int):
        self.ct = {}
        self.d = defaultdict(OrderedDict)
        self.m = capacity
        self.minw = 0

    def pop(self, key):
        w = self.ct[key]
        val = self.d[w].pop(key)
        if not self.d[w] and self.minw==w:
            self.minw = w+1
        return w,val

    def get(self, key: int) -> int:
        if key not in self.ct:
            return -1
        w,val = self.pop(key)
        self.d[w+1][key] = val
        self.ct[key] = w+1
        return val

    def put(self, key: int, value: int) -> None:
        if key in self.ct:
            w,_ = self.pop(key)
            self.d[w+1][key] = value
            self.ct[key] = w+1
        else:
            if len(self.ct)==self.m:
                x = self.d[self.minw].popitem(last=False)[0]
                self.ct.pop(x)
            self.d[1][key] = value
            self.ct[key] = 1
            self.minw= 1

382 ms