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

设计LFU缓存结构

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

哈希表 + 小根堆

import java.util.*;


public class Solution {
    /**
     * lfu design
     * @param operators int整型二维数组 ops
     * @param k int整型 the k
     * @return int整型一维数组
     */
    public int[] LFU (int[][] operators, int k) {
        // write code here
        LFUCache cache = new LFUCache(k);
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < operators.length; i++) {
            if (operators[i][0] == 1) {
                cache.set(operators[i][1], operators[i][2]);
            } else {
                ans.add(cache.get(operators[i][1]));
            }
        }
        int[] arr = new int[ans.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = ans.get(i);
        }
        return arr;
    }

    class Node {
        int key;
        int value;
        int times;
        int timestamp;

        public Node(int key, int value, int times, int timestamp) {
            this.key = key;
            this.value = value;
            this.times = times;
            this.timestamp = timestamp;
        }
    }

    class LFUCache {
        int capacity;
        int size;
        int stamp;
        Hashtable<Integer, Node> table;
        PriorityQueue<Node> minHeap;
        public LFUCache(int capacity) {
            table = new Hashtable<>(capacity);
            this.capacity = capacity;
            this.minHeap = new PriorityQueue<>(capacity, (o1, o2) ->
                    o1.times == o2.times ? o1.timestamp - o2.timestamp : o1.times - o2.times);
        }
        public void set(int key, int value) {
            Node node = table.get(key);
            if (node == null) {
                if (size == capacity) {
                    Node remove = minHeap.poll();
                    table.remove(remove.key);
                    size--;
                }
                node = new Node(key, value, 0, stamp++);
                minHeap.add(node);
                table.put(key, node);
                size++;
            } else {
                node.value = value;
                update(node);
            }
        }

        public int get(int key) {
            Node node = table.get(key);
            if (node == null) {
                return -1;
            }
            update(node);
            return node.value;
        }
        private void update(Node node) {
            node.times++;
            node.timestamp = ++stamp;
            heapfy(node);
        }
        private void heapfy(Node node) {
            minHeap.remove(node);
            minHeap.add(node);
        }
    }
}
全部评论

相关推荐

大方的大熊猫准备进厂:1.教育背景:你希望从事什么专业的工作你的主修课就是什么;成绩优秀是你应该做的,没什么可描述的,成绩不优秀也许人家在大学忙着创业呢?(成绩优秀不一定是好事,只能说明多元化的大学你上成了高中,没有真正上明白大学,反而体现了你死板,不爱社交,没有别的突出能力) 2.实践经历:你想表达的意思没有说清楚。你是说你会个性化服务,还是你有实习经历。如果没有带来,经济收益,表彰,更好的发展前景,那你还不如说说提升了自己哪些技能。你说有人给你送锦旗我都能明白你优秀,但是你说你会xxxx,你说这话谁信,证据呢。 3.入伍经历:你描述的就是你的工作职责或者你应该做的,并没有体现出来你把这个事情做好了,而且入伍经历并不能证明你能干好你要应聘的工作,不如只写经历其余所有内容都不写。 4.荣誉技能:重点突出一下,但不要过多描述,这些荣誉的含金量懂得都懂。 重点:你要应聘什么工作(具体岗位,实习生不具体),你的期望薪资
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 10:56
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务