题解 | #LFU缓存结构设计#

LFU缓存结构设计

http://www.nowcoder.com/practice/93aacb4a887b46d897b00823f30bfea1

java版本

核心:双Hashmap, 一个用作set,get操作,一个用作队列。

用作队列的HashMap 键为操作的次数,值为一个双链表链接所有具有相同操作的节点。

注意有插入和删除操作时应先判断 键 是否存在,删除最不常使用节点时应判断链表是否为空,为空应删除。

import java.util.*;


public class Solution {
    /**
     * lfu design
     * @param operators int整型二维数组 ops
     * @param k int整型 the k
     * @return int整型一维数组
     */
    class Node{
        private int key, value, cnt;
        private Node pre, next;

        public Node(int key, int value){
            this.key = key;
            this.value = value;
            this.cnt = 1;
            this.pre = this.next = null;
        }
    }

    class Que{
        private Node head;
        private Node tail;

        public Que(Node head, Node tail){
            this.head = head;
            this.tail = tail;
            this.head.next = this.tail;
            this.tail.pre = this.head;
        }
    }

    class Lfu{
        private HashMap<Integer, Node> hashmap;
        private HashMap<Integer, Que> que;
        int size;
        int least;

        public Lfu(int size){
            this.hashmap = new HashMap<>();
            this.que = new HashMap<>();
            this.size = size;
            this.least = 1;
        }

        public void refresh(Node node){
            if(this.que.containsKey(node.cnt)){
                    Que q1 = this.que.get(node.cnt);
                    node.next = q1.head.next;
                    node.pre = q1.head;
                    q1.head.next.pre = node;
                    q1.head.next = node;
                }else{
                    Que q2 = new Que(new Node(-1, -1), new Node(-1, -1));
                    node.next = q2.head.next;
                    node.pre = q2.head;
                    node.next.pre = node;
                    q2.head.next = node;
                    this.que.put(node.cnt, q2);
                }
        }

        public void set(int key, int value){
            if(get(key) != -1){
                Node node = this.hashmap.get(key);
                node.value = value;
                node.cnt += 1;
                this.hashmap.put(key, node);
            }else{
                if(this.size == 0){
                    Que q = this.que.get(this.least);
                    this.hashmap.remove(q.tail.pre.key);
                    q.tail.pre.pre.next = q.tail;
                    q.tail.pre = q.tail.pre.pre;

                    if(q.head.next == q.tail){
                        this.que.remove(this.least);
                        while(!this.que.containsKey(this.least)) this.least++;
                    }
                }else{
                    this.size --;
                }
                Node node = new Node(key, value);
                this.hashmap.put(key, node);
                this.least = 1;

                refresh(node);
            }
        }

        public int get(int key){
            if(hashmap.containsKey(key)){
                Node node = this.hashmap.get(key);
                node.cnt ++;
                node.pre.next = node.next;
                node.next.pre = node.pre;
                refresh(node);
                return node.value;
            }
            return -1;
        }
    }

    public int[] LFU (int[][] operators, int k) {
        // write code here
        ArrayList<Integer> arraylist = new ArrayList<>();
        int[] res;
        Lfu lfu = new Lfu(k);
        for(int i = 0; i < operators.length; i++){
            if(operators[i][0] == 1){
                lfu.set(operators[i][1], operators[i][2]);
            }else{
                arraylist.add(lfu.get(operators[i][1]));
            }
        }
        res = new int[arraylist.size()];
        for(int i = 0; i < arraylist.size(); i++) res[i] = arraylist.get(i);
        return res;
    }
}
全部评论
[[1,1,1],[2,1],[1,2,2],[2,1]],1 这个样例通不过 最小次数least并没有正确更新,80行更新后,87行立马置1,这是有问题的,还有其他细节
点赞 回复 分享
发布于 2021-04-16 20:49

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
9068次浏览 83人参与
# 你的实习产出是真实的还是包装的? #
1662次浏览 40人参与
# 巨人网络春招 #
11296次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7382次浏览 40人参与
# 重来一次,我还会选择这个专业吗 #
433301次浏览 3926人参与
# 简历第一个项目做什么 #
31500次浏览 327人参与
# 米连集团26产品管培生项目 #
5603次浏览 214人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186885次浏览 1118人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152269次浏览 887人参与
# 研究所笔面经互助 #
118851次浏览 577人参与
# 简历中的项目经历要怎么写? #
309944次浏览 4189人参与
# 面试紧张时你会有什么表现? #
30473次浏览 188人参与
# 你今年的平均薪资是多少? #
212980次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63310次浏览 798人参与
# 我的求职精神状态 #
447961次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76415次浏览 374人参与
# 高学历就一定能找到好工作吗? #
64294次浏览 620人参与
# 牛客AI文生图 #
21399次浏览 238人参与
# 你怎么看待AI面试 #
179799次浏览 1229人参与
# 正在春招的你,也参与了去年秋招吗? #
363190次浏览 2636人参与
# 腾讯音乐求职进展汇总 #
160562次浏览 1109人参与
# 职能管理面试记录 #
10795次浏览 59人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务