leetcode 460 LFU缓存

原题点这里

class Node implements Comparable<Node>{
    public int key;
    public int value;
    public int lastTime;
    public int fre;
    public Node(int key,int value,int lastTime){
        this.key=key;
        this.value=value;
        this.lastTime = lastTime;
        this.fre=1;
    }

    @Override
    public int compareTo(Node o) {

        if(this.fre!=o.fre){
            //System.out.println(this.key+"++++fre:"+this.fre);
            //System.out.println(o.key+"+++++fre"+o.fre);
            //System.out.println(Integer.compare(o.fre,this.fre));
            return Integer.compare(this.fre,o.fre);
        }
        //System.out.println(this.key+"---"+this.value);
        //System.out.println(o.key+"----"+o.value);
        //System.out.println(-Integer.compare(this.lastTime,o.lastTime));
        return Integer.compare(this.lastTime,o.lastTime);
    }

    public void setValue(int value,int lastTime) {
        this.value=value;
        this.lastTime=lastTime;
        this.fre++;
    }

    public int getValue(int lastTime) {
        this.lastTime=lastTime;
        this.fre++;
        return value;

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node node = (Node) o;
        return key == node.key;

    }

    @Override
    public int hashCode() {
        return Objects.hash(key, value);
    }
}
class LFUCache {
    public int capacity;
    public int time;
    public HashMap<Integer,Node> values;
    public TreeSet<Node> keys;
    public LFUCache(int capacity) {
        time=0;
        this.capacity=capacity;
        values = new HashMap<Integer, Node>();
        keys = new TreeSet<Node>();

    }

    public int get(int key) {
        if(capacity==0) return -1;
        if(values.containsKey(key)){
            Node oldN = values.get(key);
            keys.remove(oldN);
            int ans = oldN.getValue(++time);
            keys.add(oldN);
            return ans;
        }

        else return -1;

    }

    public void put(int key, int value) {
        if(capacity==0) return;
        if(values.containsKey(key)){
            Node oldN = values.get(key);
            keys.remove(oldN);
            oldN.setValue(value,++time);
            keys.add(oldN);

        }else{
            if(values.size()==this.capacity) {
                Node n = values.get(keys.first().key);
                values.remove(n.key);
                keys.remove(n);
            }

            Node s = new Node(key,value,++time);
            values.put(key,s);
            keys.add(s);

        }

    }

}

原文地址:https://www.cnblogs.com/superxuezhazha/p/12643859.html

时间: 2024-11-10 13:58:16

leetcode 460 LFU缓存的相关文章

[LeetCode] 460. LFU Cache 最近最不常用页面置换缓存器

Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.pu

LeetCode &quot;460. LFU Cache&quot; !

My first try was very close to a final solution .. however, this is a much neater solution: https://discuss.leetcode.com/topic/69436/concise-c-o-1-solution-using-3-hash-maps-with-explanation Lesson learnt: data structure is crucial. And, if some logi

460. LFU缓存

题目地址:https://leetcode-cn.com/problems/lfu-cache/submissions/ 代码地址:https://github.com/javartisan/edx-ds/blob/master/src/main/java/com/javartisan/leetcode/LFUCache460.java class LFUCache { private static class Node{ public int key=-1; public int value=

460. LFU Cache

460. LFU Cache Total Accepted: 5305 Total Submissions: 26292 Difficulty: Hard Contributors: 1337c0d3r, fishercoder Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: getand put. ge

-实现 LFU 缓存算法

-实现 LFU 缓存算法, 设计一个类 LFUCache,实现下面三个函数 + 构造函数: 传入 Cache 内最多能存储的 key 的数量 + get(key):如果 Cache 中存在该 key,则返回对应的 value 值,否则,返回-1. + set(key,value):如果 Cache 中存在该 key,则重置 value 值:如果不存在该 key,则将该 key 插入到到 Cache 中,若插入后会导致 Cache 中存储的 key 个数超过最大容量,则在插入前淘汰访问次数最少的数

【Leetcode刷题】LFU缓存

题目:https://leetcode-cn.com/problems/lfu-cache/ 思路: O(1)的数据结构:hashmap 维持最近使用:OrderdDict(详见LRU缓存问题) 使用一个hashmap维系key到出现频率的映射关系 另一个hashmap维系频率到数据(key-value键值对)的关系 由于 当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最近 最少使用的键.因此key-value键值对应该用OrderdDict存储 import collection

LFU缓存

https://leetcode-cn.com/problems/lfu-cache/description/ 缓存的实现可以采取多种策略,不同策略优点的评估就是"命中率".好的策略可以实现较高的命中率.常用的策略如:LRU(最近最少使用).LFU(最不频繁使用).这两种策略都可以在O(1)时间内实现get和put.本文主要讲讲LFU的实现. import java.util.HashMap; import java.util.LinkedHashSet; class LFUCache

[LeetCode]146.LRU缓存机制

设计和实现一个 LRU(最近最少使用)缓存 数据结构,使它应该支持以下操作: get 和 put . get(key) - 如果密钥存在于缓存中,则获取密钥的值(总是正数),否则返回 -1.put(key, value) - 如果密钥不存在,请设置或插入值.当缓存达到其容量时,它应该在插入新项目之前使最近最少使用的项目作废. 后续: 你是否可以在 O(1) 时间复杂度中进行两种操作?注:这道题也是2018今日头条春招面试题. 案例: LRUCache cache = new LRUCache(

[LeetCode] LRU Cache 缓存器

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set