力扣labuladong——一刷day89

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


LFU 算法是要复杂很多的,而且经常出现在面试中,因为 LFU 缓存淘汰算法在工程实践中经常使用,也有可能是因为 LRU 算法太简单了。不过话说回来,这种著名的算法的套路都是固定的,关键是由于逻辑较复杂,不容易写出漂亮且没有 bug 的代码

一、力扣460. LFU 缓存

class LFUCache {
   
    private int cap;
    private int minFreq;
    private HashMap<Integer,Integer> keyToVal;
    private HashMap<Integer,Integer> keyToFreq;
    private HashMap<Integer,LinkedHashSet<Integer>> freqToKeys;

    public LFUCache(int capacity) {
   
        this.cap = capacity;
        minFreq = 0;
        keyToVal = new HashMap<>();
        keyToFreq = new HashMap<>();
        freqToKeys = new HashMap<>();
    }
    
    public int get(int key) {
   
        if(!keyToVal.containsKey(key)){
   
            return -1;
        }
        increase(key);
        return keyToVal.get(key);
    }
    
    public void put(int key, int value) {
   
        if(cap <= 0)return;
        if(keyToVal.containsKey(key)){
   
            keyToVal.put(key,value);
            increase(key);
            return;
        }
        if(cap <= keyToVal.size()){
   
            removeMinFreq();
        }
        keyToVal.put(key,value);
        keyToFreq.put(key,1);
        freqToKeys.putIfAbsent(1,new LinkedHashSet<Integer>());
        freqToKeys.get(1).add(key);
        this.minFreq = 1;
    }
    private void increase(int key){
   
        int freq = keyToFreq.get(key);
        keyToFreq.put(key,freq+1);
        freqToKeys.get(freq).remove(key);
        freqToKeys.putIfAbsent(freq+1,new LinkedHashSet<Integer>());
        freqToKeys.get(freq+1).add(key);
        if(freqToKeys.get(freq).isEmpty()){
   
            freqToKeys.remove(freq);
            if(freq == minFreq){
   
                this.minFreq ++;
            }
        }
    }
    private void removeMinFreq(){
   
        LinkedHashSet<Integer> list = freqToKeys.get(this.minFreq);
        int key = list.iterator().next();
        list.remove(key);
        if(list.isEmpty()){
   
            freqToKeys.remove(this.minFreq);
        }
        keyToFreq.remove(key);
        keyToVal.remove(key);
    }
}

/**
 * Your LFUCache object will be instantiated and called as such:
 * LFUCache obj = new LFUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */

相关推荐

  1. labuladong——day89

    2024-01-10 21:36:02       34 阅读
  2. labuladong——day80

    2024-01-10 21:36:02       33 阅读
  3. labuladong——day81

    2024-01-10 21:36:02       39 阅读
  4. labuladong——day84

    2024-01-10 21:36:02       36 阅读
  5. labuladong——day87

    2024-01-10 21:36:02       38 阅读
  6. labuladong——day68

    2024-01-10 21:36:02       39 阅读
  7. labuladong——day67

    2024-01-10 21:36:02       35 阅读
  8. labuladong——day69

    2024-01-10 21:36:02       37 阅读
  9. labuladong——day66

    2024-01-10 21:36:02       33 阅读
  10. labuladong——day70

    2024-01-10 21:36:02       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 21:36:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 21:36:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 21:36:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 21:36:02       20 阅读

热门阅读

  1. gradient_checkpointing

    2024-01-10 21:36:02       32 阅读
  2. IC设计的前端和后端是如何区分的?

    2024-01-10 21:36:02       42 阅读
  3. 关于MySQL源码的学习 这里是一些建议

    2024-01-10 21:36:02       34 阅读
  4. python工具-udp-tcp-client-server-demo

    2024-01-10 21:36:02       35 阅读
  5. 【React】常见疑问的整理

    2024-01-10 21:36:02       33 阅读