题目:

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

分析:

设计并实现最不经常使用(LFU)缓存的数据结构。相比LRU,LFU增加了频率这一概念,在cache满了的时候,将会把最不经常使用的缓存清除掉,在本题则是以访问频率体现的。

利用hashmap来记录当前数据{key, value}和其出现次数之间的映射,我们可以把出现频率相同的key都放到一个list集合中,再使用一个hashmap建立频率和list之间的映射。为了能够在O(1)的时间内完成操作了,要快速的定位list中key的位置,我们再用一个hashmap来建立key和freq中key的位置之间的映射。最后当然我们还需要两个变量cap和minFreq,分别来保存cache的大小,和当前最小的频率。

我们假设容量为2,先put(1, 1)和put(2, 2),此时容器的存储情况如下:

此时调用get(1,1),我们需要做的操作如下:

1.如果m中不存在key1,那么返回-1

2. 从freq中频率为1(从m中获取到的key对应的频率值)的list中将1删除

3. 将m中1对应的频率值自增1

4. 将1保存到freq中频率为2(当前频率加+1)的list的末尾

5. 在l中保存1在freq中频率为2的list中的位置

6. 如果freq中频率为minFreq的list为空,minFreq自增1

7. 返回m中1对应的value值

经过这些步骤后,我们再来看下此时内部容器情况:

此时再put(3, 3),我们需要做的操作如下:

1. 先调用get(3)返回的结果不是-1,也就是说此时cache中已经存在3这块缓存了,我们可以直接将m中3对应的value更新为当前value,并返回。

2. 如果此时m的大小大于了cap,即超过了cache的容量,则:

  a)在m中移除minFreq对应的list的首元素的key。

  b)在l中清除3对应的纪录

  c)在freq中移除minFreq对应的list的首元素。

3. 在m中建立3的映射,即 3 -> {value 3, frequent1}

4. 在freq中频率为1的list末尾加上3

5. 在l中保存3在freq中频率为1的list中的位置

6. minFreq重置为1

此时内部容器情况:

虚线表示此步骤删去的元素。

程序:

C++

class LFUCache {
public:
LFUCache(int capacity) {
cap = capacity;
} int get(int key) {
if(m.count(key) == 0)
return -1;
freq[m[key].second].erase(l[key]);
m[key].second++;
freq[m[key].second].push_back(key);
l[key] = --freq[m[key].second].end();
if(freq[minFreq].size() == 0)
minFreq++;
return m[key].first;
} void put(int key, int value) {
if(cap <= 0)
return;
if(get(key) != -1){
m[key].first = value;
return;
}
if(m.size() >= cap){
m.erase(freq[minFreq].front());
l.erase(freq[minFreq].front());
freq[minFreq].pop_front();
}
m[key] = make_pair(value, 1);
freq[1].push_back(key);
l[key] = --freq[1].end();
minFreq = 1;
} private:
int cap, minFreq;
unordered_map<int, pair<int, int>> m;
unordered_map<int, list<int>> freq;
unordered_map<int, list<int>::iterator> l;
}; /**
* 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);
*/

Java

public class LFUCache {

    public LFUCache(int capacity) {
cap = capacity;
map = new HashMap<>();
freq = new HashMap<>();
list = new HashMap<>();
minFreq = 1;
}
public int get(int key) {
if(!map.containsKey(key))
return -1;
int count = freq.get(key);
list.get(count).remove(key);
freq.put(key, count + 1);
if(list.get(minFreq).size() == 0)
minFreq++;
if(!list.containsKey(count + 1)){
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
set.add(key);
list.put(count+1, set);
}else{
list.get(count+1).add(key);
}
return map.get(key);
}
public void put(int key, int value) {
if(cap <= 0)
return;
if(get(key) != -1){
map.put(key, value);
return;
}
if(map.size() >= cap){
Integer removeKey = list.get(minFreq).iterator().next();
map.remove(removeKey);
list.get(minFreq).remove(removeKey);
freq.remove(removeKey);
}
//System.out.println(map.toString());
map.put(key, value);
freq.put(key, 1);
if(!list.containsKey(1)){
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
set.add(key);
list.put(1, set);
}else{
list.get(1).add(key);
}
minFreq = 1;
} private int cap;
private int minFreq;
private HashMap<Integer, Integer> map;
private HashMap<Integer, Integer> freq;
private HashMap<Integer, LinkedHashSet<Integer>> list;
} /**
* 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);
*/

LeetCode 460. LFU Cache LFU缓存 (C++/Java)的更多相关文章

  1. LeetCode:146_LRU cache | LRU缓存设计 | Hard

    题目:LRU cache Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

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

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

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

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  4. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  5. Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  6. LeetCode LFU Cache

    原题链接在这里:https://leetcode.com/problems/lfu-cache/?tab=Description 题目: Design and implement a data str ...

  7. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  8. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. LFU Cache

    2018-11-06 20:06:04 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频率也更高”. ...

  10. LRU缓存实现(Java)

    LRU Cache的LinkedHashMap实现 LRU Cache的链表+HashMap实现 LinkedHashMap的FIFO实现 调用示例 LRU是Least Recently Used 的 ...

随机推荐

  1. 2024-04-21:用go语言,给一棵根为1的树,每次询问子树颜色种类数。 假设节点总数为n,颜色总数为m, 每个节点的颜色,依次给出,整棵树以1节点做头, 有k次查询,询问某个节点为头的子树,一共

    2024-04-21:用go语言,给一棵根为1的树,每次询问子树颜色种类数. 假设节点总数为n,颜色总数为m, 每个节点的颜色,依次给出,整棵树以1节点做头, 有k次查询,询问某个节点为头的子树,一共 ...

  2. 暑期集训 Day11 —— 模拟赛复盘

    ${\color{Green} \mathrm{Problem\ 1 :Subarray }} $ 签到失败... 直接二进制分组,找出所有二进制位=0 的方法. 死因: 二进制分组没想出来... $ ...

  3. 技术干货 | Native 页面下如何实现导航栏的定制化开发?

    ​简介: 通过不同实际场景的描述,供大家参考完成 Native 页面的定制化开发. ​ 很多 mPaaS Coder 在接入 H5 容器后都会对容器的导航栏进行深度定制,本文旨在通过不同实际场景的描述 ...

  4. dotnet 推荐 LightWorkFlowManager 轻量的工作过程管理库

    本文将和大家推荐我团队开源的 LightWorkFlowManager 轻量的工作过程管理库,适合任何需要执行工作过程的应用逻辑,可以方便将多个工作过程拼凑起来,且自动集成重试和失败处理,以及日志和上 ...

  5. Redis官方开源的可视化管理工具 - RedisInsight

    前言 今天大姚给大家推荐一款Redis官方开源的可视化管理工具:RedisInsight. Redis介绍 Redis (Remote Dictionary Server) 是一个使用 C 语言编写的 ...

  6. 利用PostMan 模拟上传/下载文件

    我们经常用postman模拟各种http请求.但是有时候因为业务需要,我们需要测试上传下载功能.其实postman也是很好支持这两种操作的. 一.上传文件: 1.打开postman 选择对应reque ...

  7. git checkout 命令图文详解

    目录 git checkout branchname (切换本地分支) 切换远程分支 放弃修改 git checkout . git checkout – filename git checkout ...

  8. gin返回json假数据

    package main import ( "github.com/gin-gonic/gin" "encoding/json" "fmt" ...

  9. 🔥 Java Solon v2.7.6 发布

    Java Solon 是什么框架? Java "新的"应用开发框架.开放原子开源基金会,孵化项目.从零开始构建(非 java-ee 架构),有灵活的接口规范与开放生态. 追求: 更 ...

  10. mogodb的使用语句(命令)大全

    官网学习网址:https://docs.mongodb.com/ mongo库表操作语句实际操作过程 1: mongo入门命令 1.1: show dbs 查看当前的数据库 1.2 use datab ...