• 2023-09-25 LeetCode每日一题(LFU 缓存)


    2023-09-25每日一题

    一、题目编号

    460. LFU 缓存
    
    • 1

    二、题目链接

    点击跳转到题目位置

    三、题目描述

    请你为 最不经常使用(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) 的平均时间复杂度运行。

    示例:
    在这里插入图片描述提示:

    • 1 <= capacity <= 104
    • 0 <= key <= 105
    • 0 <= value <= 109
    • 最多调用 2 * 105 次 get 和 put 方法

    四、解题代码

    struct Node {
        int cnt, time, key, value;
    
        Node(int _cnt, int _time, int _key, int _value):cnt(_cnt), time(_time), key(_key), value(_value){}
        
        bool operator < (const Node& rhs) const {
            return cnt == rhs.cnt ? time < rhs.time : cnt < rhs.cnt;
        }
    };
    class LFUCache {
        // 缓存容量,时间戳
        int capacity, time;
        unordered_map<int, Node> key_table;
        set<Node> S;
    public:
        LFUCache(int _capacity) {
            capacity = _capacity;
            time = 0;
            key_table.clear();
            S.clear();
        }
        
        int get(int key) {
            if (capacity == 0) return -1;
            auto it = key_table.find(key);
            // 如果哈希表中没有键 key,返回 -1
            if (it == key_table.end()) return -1;
            // 从哈希表中得到旧的缓存
            Node cache = it -> second;
            // 从平衡二叉树中删除旧的缓存
            S.erase(cache);
            // 将旧缓存更新
            cache.cnt += 1;
            cache.time = ++time;
            // 将新缓存重新放入哈希表和平衡二叉树中
            S.insert(cache);
            it -> second = cache;
            return cache.value;
        }
        
        void put(int key, int value) {
            if (capacity == 0) return;
            auto it = key_table.find(key);
            if (it == key_table.end()) {
                // 如果到达缓存容量上限
                if (key_table.size() == capacity) {
                    // 从哈希表和平衡二叉树中删除最近最少使用的缓存
                    key_table.erase(S.begin() -> key);
                    S.erase(S.begin());
                }
                // 创建新的缓存
                Node cache = Node(1, ++time, key, value);
                // 将新缓存放入哈希表和平衡二叉树中
                key_table.insert(make_pair(key, cache));
                S.insert(cache);
            }
            else {
                // 这里和 get() 函数类似
                Node cache = it -> second;
                S.erase(cache);
                cache.cnt += 1;
                cache.time = ++time;
                cache.value = value;
                S.insert(cache);
                it -> second = cache;
            }
        }
    };
    
    • 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
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    五、解题思路

    (1) 哈希表+二叉平衡树

  • 相关阅读:
    网络规划设计师之OSI七层模型之应用层
    Redis事务管理
    【c++百日刷题计划】 ———— DAY8,奋战百天,带你熟练掌握基本算法
    Vue2.0简讲!
    Spark学习(3)-Spark环境搭建-Standalone
    python判断字符串是否只由字母或数字组成——isalnum函数 的用法及实例
    yolov5量化注意事项
    pytorch查看网络架构的几种方法
    Flume配置3——拦截器过滤
    美联储历次加息周期及结果
  • 原文地址:https://blog.csdn.net/qq_56086076/article/details/133279250