leetcode460. LFU缓存

请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。它应该支持以下操作:get 和 put。
get(key) - 如果键存在于缓存中,则获取键的值(总是正数),否则返回 -1。
put(key, value) - 如果键已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量时,则应该在插入新项之前,使最不经常使用的项无效。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最近 最少使用的键。
「项的使用次数」就是自插入该项以来对其调用 get 和 put 函数的次数之和。使用次数会在对应项被移除后置为 0 。

进阶:
你是否可以在 O(1) 时间复杂度内执行两项操作?

示例:
LFUCache cache = new LFUCache( 2 /* capacity (缓存容量) */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 去除 key 2
cache.get(2); // 返回 -1 (未找到key 2)
cache.get(3); // 返回 3
cache.put(4, 4); // 去除 key 1
cache.get(1); // 返回 -1 (未找到 key 1)
cache.get(3); // 返回 3
cache.get(4); // 返回 4

答:
其实着道题的意思理解之后,就可以有思路了。
就是按照使用次数times排序,当times一致时,谁更晚操作就把谁放在后面。
本来计划使用PriorityQueue的,但是只有在删除最小节点和插入时较快(也是O(lgn),不是O(1)),对于查询很慢很慢。
第二反应是,使用TreeMap+链表的方式,TreeMap的key是times(应该是可行的)。
最后使用的是使用链表保存数据,保持链表有序(以times排序)。
光只有链表也不行,每次移动可能会要很久,所以加入lastMap,来进行快速移动(比如,我的node的times由3改成4,那我就移动到last(4)后面)。

代码:

class LFUCache {
    Node head;
    int capacity;
    int sum = 0;
    //保存key和Node的关系
    Map<Integer, Node> ansMap;
    //保存times和Node关系(每次移动节点时候,知道移动到哪)
    Map<Integer, Node> lastMap;
    //当capacity为1时,直接使用
    int key = 0;
    int value = -1;

    public LFUCache(int capacity) {
        this.capacity = capacity;
        if (capacity <= 1) {
            return;
        } else {
            ansMap = new HashMap<>(capacity);
            lastMap = new HashMap<>();
        }
    }

    public int get(int key) {
        if (capacity <= 0) {
            return -1;
        } else if (capacity == 1) {
            if (key == this.key) {
                return value;
            } else {
                return -1;
            }
        }
        Node ans = ansMap.get(key);
        if (ans != null) {
            //校验是否在lastMap中
            checkLast(ans);
            //次数+1
            ans.times++;
            //移动node的位置
            moveNext(ans);
        } else {
            return -1;
        }
        return ans.value;
    }

    public void put(int key, int value) {
        if (capacity <= 0) {
            return;
        }
        if (capacity == 1) {
            this.key = key;
            this.value = value;
            return;
        }
        Node node = new Node();
        node.times = 1;
        node.key = key;
        node.value = value;
        if (head == null) {
            //head
            head = node;
            ansMap.put(key, node);
            lastMap.put(1, node);
            sum++;
        } else {
            Node ans = ansMap.get(key);
            if (ans != null) {
                //这时候的操作其实和get差不多,除了更新value值
                checkLast(ans);
                ans.times++;
                ans.value = value;
                moveNext(ans);
            } else {
                if (sum >= capacity) {
                    //我只保存capacity个数据,所以要移除head
                    ansMap.remove(head.key);
                    if (head.times != head.next.times) {
                        lastMap.remove(head.times);
                    }
                    head = head.next;
                }
                ansMap.put(key, node);
                if (head.times > 1) {
                    //我要插在首位了
                    node.next = head;
                    head.pre = node;
                    head = node;
                    lastMap.put(1, head);
                } else {
                    addNext(lastMap.get(1), node);
                }
                sum++;
            }
        }
    }

    //如果属于lastMap,进行删除
    private void checkLast(Node node) {
        if (lastMap.get(node.times) == node) {
            if (node.pre != null && node.pre.times == node.times) {
                lastMap.put(node.times, node.pre);
            } else {
                lastMap.remove(node.times);
            }
        }
    }

    //在nodeL后面插入node
    private void addNext(Node nodeL, Node node) {
        lastMap.put(node.times, node);
        if (nodeL.next == null) {
            nodeL.next = node;
            node.pre = nodeL;
            node.next = null;
            return;
        } else {
            nodeL.next.pre = node;
            node.next = nodeL.next;
            nodeL.next = node;
            node.pre = nodeL;
        }
    }

    private void moveNext(Node node) {
        Node next = node.next;
        if (next == null || node.times < next.times) {
            //无需移动,只要修改lastMap
            lastMap.put(node.times, node);
            return;
        }
        //移除node位置
        if (node == head) {
            head = node.next;
        } else {
            node.pre.next = node.next;
            node.next.pre = node.pre;
        }
        Node add = lastMap.get(node.times);
        if (add == null) {
            //都是k次,且不是last,恰好k+1次无数据,这时候插在last(k)后面
            addNext(lastMap.get(node.times - 1), node);
        } else {
            //插在add后面
            addNext(add, node);
        }
    }
}

class Node {
    int times;
    int key;
    int value;
    Node pre;
    Node next;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值