目录

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
33
class LFUCache:

    def __init__(self, capacity: int):
        from sortedcontainers import SortedList
        self.sl = SortedList()
        self.d = {}
        self.k = capacity
        self.t = 0
        
    def get(self, key: int) -> int:
        if key not in self.d:
            return -1
        w,t,val = self.d[key]
        self.sl.remove((w,t,key))
        self.t += 1
        self.d[key] = (w+1,self.t,val)
        self.sl.add((w+1,self.t,key))
        return val

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

284 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

168 ms

#3

更通用的双向链表写法

解答

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Node:
    # 提高访问属性的速度,并节省内存
    __slots__ = 'pre', 'nxt', 'key', 'val', 'w'

    def __init__(self, key=0, val=0):
        self.key = key
        self.val = val
        self.pre = self
        self.nxt = self
        self.w = 1

    def insert(self,x):
        q = self.nxt
        self.nxt = x
        x.pre = self
        x.nxt = q
        q.pre = x
    
    def remove(self,):
        self.pre.nxt = self.nxt
        self.nxt.pre = self.pre


class LFUCache:

    def __init__(self, capacity: int):
        self.c = capacity
        self.map = {}
        self.d = defaultdict(lambda: Node())
        self.miw = 0

    def update(self,node):
        w = node.w
        node.remove()
        if self.d[w].pre==self.d[w] and w==self.miw:
            self.miw += 1
        self.d[w+1].insert(node)
        node.w += 1
        
    def get(self, key: int) -> int:
        if key not in self.map:
            return -1
        node = self.map[key]
        self.update(node)
        return node.val

    def put(self, key: int, value: int) -> None:
        if key in self.map:
            node = self.map[key]
            self.update(node)
            node.val = value
            return
        if len(self.map)==self.c:
            dum = self.d[self.miw]
            p = dum.pre
            del self.map[p.key]
            p.remove()
        self.map[key] = node = Node(key,value)
        self.d[1].insert(node)
        self.miw = 1

148 ms