博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
460. LFU Cache
阅读量:5112 次
发布时间:2019-06-13

本文共 3701 字,大约阅读时间需要 12 分钟。

Design and implement a data structure for  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.

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 1cache.put(3, 3);    // evicts key 2cache.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 3cache.get(4);       // returns 4

解题思路:

I use two structure to solve this problem

list<pair<int, list<int>>> records the visit info(count & time) of each key, let's call it big-L

the most inner <int> is the key, and list<int> rank them according to visit-time, from old to new.
pair<int, list<int>> associate each list<int> with visit-count
e.g. {2, {a, b}} means both a & b has been visited for 2 times, but b has a newer last-visit-time

and the other structure unordered_map<int, info> keeps the value and iterators in big-L of each key

so everytime we have to remove one element, we remove the first <int> of first pair<int, list<int>> in big-L

and everytime we have to insert one element, just check if the first pair's count equals 1. If it is, use that pair, push new element to its back; otherwise, insert a new pair.

and everytime an element is visited, we move it from the old pair to the next one in big-L. if no following pair exists, or following pair's count is not old-count+1, just insert a new pair with old-count+1. don't forget to update the iterators in map :D

横向一个频率(从小到大)链表,纵向一个LRU(从旧到新),

class LFUCache {public:    struct info{        int val;        list
>>::iterator it_pair; list
::iterator it_key; }; LFUCache(int capacity) { _capacity=capacity; } int get(int key) { auto it = cache.find(key); if(it == cache.end())return -1; touch(it,key); return it->second.val; } void put(int key, int value) { auto it = cache.find(key); if(it != cache.end()){ touch(it,key); it->second.val=value; } else { if(_capacity==0)return ; if(cache.size() == _capacity){ auto it=Flist.front().second.begin(); cache.erase(*it); Flist.front().second.erase(it); if(Flist.front().second.size()==0) Flist.pop_front(); } if(Flist.empty()||Flist.front().first!=1) Flist.push_front({
1,{key}}); else Flist.front().second.emplace_back(key); cache[key]={value, Flist.begin(),std::prev(Flist.front().second.end())}; } }private: int _capacity; list
>>Flist;//Frequency List unordered_map
cache; void touch(unordered_map
::iterator it,int key){ auto it_key=it->second.it_key; auto it_pair=it->second.it_pair; int freq=it_pair->first+1; it_pair->second.erase(it_key); if(it_pair->second.size()==0){ it_pair=Flist.erase(it_pair); } else std::advance(it_pair,1); if(it_pair==Flist.end()||it_pair->first!=freq) it_pair=Flist.insert(it_pair,{freq,{key}}); else it_pair->second.emplace_back(key); it->second.it_pair=it_pair; it->second.it_key=std::prev(it_pair->second.end());//it_pair->second.back(); }};/** * 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); */

 

转载于:https://www.cnblogs.com/tsunami-lj/p/6736450.html

你可能感兴趣的文章
可选参数的函数还可以这样设计!
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
收集WebDriver的执行命令和参数信息
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>