鱼C论坛

 找回密码
 立即注册
查看: 1435|回复: 0

[学习笔记] Leetcode 460. LFU Cache

[复制链接]
发表于 2020-9-25 22:33:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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.
put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.

Note that the number of times an item is used is the number of calls to the get and put functions for that item since it was inserted. This number is set to zero when the item is removed.



Follow up:
Could you do both operations in O(1) time complexity?



Example:

LFUCache cache = new LFUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.get(3);       // returns 3.
cache.put(4, 4);    // evicts key 1.
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

  1. class LFUCache {
  2.    
  3.     public class Node{
  4.         int key;
  5.         int val;
  6.         int cnt;
  7.         Node prev;
  8.         Node next;
  9.         
  10.         public Node(int key, int val){
  11.             this.key = key;
  12.             this.val = val;
  13.             this.cnt = 1;
  14.         }
  15.     }
  16.    
  17.     public class DLList{
  18.         Node head;
  19.         Node tail;
  20.         int len;
  21.         
  22.         public DLList(){
  23.             head = new Node(0, 0);
  24.             tail = new Node(0, 0);
  25.             this.head.next = tail;
  26.             this.tail.prev = head;
  27.             this.len = 0;
  28.         }
  29.         
  30.         public void remove(Node node){
  31.             node.prev.next = node.next;
  32.             node.next.prev = node.prev;
  33.             map.remove(node.key);
  34.             len--;
  35.         }
  36.         
  37.         public void add(Node node){
  38.             Node next = head.next;
  39.             head.next = node;
  40.             node.prev = head;
  41.             node.next = next;
  42.             next.prev = node;
  43.             map.put(node.key, node);
  44.             len++;
  45.         }
  46.         
  47.         public void removeTail(){
  48.             Node node = tail.prev;
  49.             remove(node);
  50.         }
  51.     }
  52.    
  53.     Map<Integer, Node> map;
  54.     Map<Integer, DLList> freq;
  55.     int capacity, size, maxFreq;

  56.     public LFUCache(int capacity) {
  57.         map = new HashMap<>();
  58.         freq = new HashMap<>();
  59.         this.capacity = capacity;
  60.         this.size = 0;
  61.         this.maxFreq = 0;
  62.     }
  63.    
  64.     public int get(int key) {
  65.         if(map.get(key) == null) return -1;
  66.         
  67.         Node node = map.get(key);
  68.         int prevfreq = node.cnt;
  69.         DLList f = freq.get(prevfreq);
  70.         f.remove(node);
  71.         
  72.         int curtfreq = prevfreq + 1;
  73.         maxFreq = Math.max(maxFreq, curtfreq);
  74.         DLList cur = freq.getOrDefault(curtfreq, new DLList());
  75.         node.cnt++;
  76.         cur.add(node);
  77.         
  78.         freq.put(prevfreq, f);
  79.         freq.put(curtfreq, cur);
  80.         
  81.         return node.val;
  82.     }
  83.    
  84.     public void put(int key, int value) {
  85.         if(capacity == 0) return;
  86.         if(map.containsKey(key)){
  87.             map.get(key).val = value;
  88.             get(key);
  89.             return;
  90.         }
  91.         else{
  92.             Node new_node = new Node(key,value);
  93.             
  94.             DLList cur = freq.getOrDefault(1, new DLList());
  95.             cur.add(new_node);
  96.             
  97.             size++;
  98.             
  99.             if(size > capacity){
  100.                 if(cur.len > 1){
  101.                     cur.removeTail();
  102.                 }
  103.                 else{
  104.                     for(int i = 2; i <= maxFreq; i++){
  105.                         if(freq.get(i) != null && freq.get(i).len > 0){
  106.                             freq.get(i).removeTail();
  107.                             break;
  108.                         }
  109.                     }
  110.                     
  111.                 }
  112.                
  113.                 size--;
  114.             }
  115.             freq.put(1, cur);
  116.         }
  117.     }
  118. }

  119. /**
  120. * Your LFUCache object will be instantiated and called as such:
  121. * LFUCache obj = new LFUCache(capacity);
  122. * int param_1 = obj.get(key);
  123. * obj.put(key,value);
  124. */
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 11:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表