leetcode 146. LRU Cache 460. LFU Cache
2017-03-15 23:05
281 查看
146. LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache.
It should support the following operations:
cache, otherwise return -1.
cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
题意:
实现一个LRU cache,最近最久未用淘汰算法,有两个操作get和put,get是访问,put是访问然后增加或者修改值,如果容量满了,淘汰最近最久未用的值。
可以使用list存储值,每次访问将先前的值删掉(如果存在),然后加入list的头。这样能保证尾部是最近最久未用的,删除的时候从尾部取就好了。因为要知道先前的位置,于是需要增加key对应的list中的位置。
注意:迭代器可以作为map的值,不能作为键。迭代器是引用,引用需初始化才能使用,不能直接比较大小。
代码:
class LRUCache
{
public:
LRUCache(int capacity)
{
maxSize = capacity;
}
int get(int key)
{
if (index.find(key) == index.end()) return -1;
int value=index[key]->second;
data.erase(index[key]);
data.push_front(make_pair(key,value));
index[key]=data.begin();
return value;
}
void put(int key, int value)
{
if (index.find(key) != index.end())
{
data.erase(index[key]);
data.push_front(make_pair(key,value));
index[key]=data.begin();
}
else
{
if (data.size() == maxSize)
{
index.erase(data.back().first);
data.pop_back();
}
data.push_front(make_pair(key,value));
index[key] = data.begin();
}
}
private:
int maxSize;
list<pair<int, int>> data;
unordered_map<int, list<pair<int, int>>::iterator> index;
};
460. LFU Cache
Design and implement a data structure for Least Frequently Used (LFU) cache. It
should support the following operations:
cache, otherwise return -1.
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:
题意:
实现一个LFU cache,最近最不常用淘汰算法,有两个操作get和put,get是访问,put是访问然后增加或者修改值,如果容量满了,淘汰最近最不常用的值。
因为涉及频度fre,所以一个map存(key,<value,fre>),根据值能取到频度。
淘汰频度最小的,每个key可能在一个频度,再建一个键为fre的map存(fre,list<key>),这样知道删除哪个key。
因为要删除list中的元素,只知道key是无法删除自己的,需要知道迭代器,在建一个map存key到list<key>迭代器的映射。
代码:
class LFUCache {
public:
int size;
unordered_map<int,pair<int,int>> mpkv;
unordered_map<int,list<int>> mpfre;
unordered_map<int,list<int>::iterator> mpiter;
int minfre;
LFUCache(int capacity) {
size=capacity;
}
int get(int key) {
if(mpkv.find(key)==mpkv.end()) return -1;
int res=mpkv[key].first;
int fre=mpkv[key].second;
mpkv[key].second=fre+1;
mpfre[fre].erase(mpiter[key]);
if(fre==minfre&&mpfre[fre].size()==0) minfre++;
if(mpfre.find(fre+1)==mpfre.end()){
mpfre[fre+1].push_back(key);
mpiter[key]=--mpfre[fre+1].end();
}
else{
mpiter[key] = mpfre[fre+1].insert(mpfre[fre+1].end(),key);
}
return res;
}
void put(int key, int value) {
if (size <= 0) return;
if(mpkv.find(key)==mpkv.end()){
//pop
if(mpkv.size()==size){
int popkey=mpfre[minfre].front();
// cout<<"popkey:"<<popkey<<endl;
mpfre[minfre].pop_front();
mpkv.erase(popkey);
mpiter.erase(popkey);
}
// cout<<"pushkey:"<<key<<" val:"<<value<<endl;
minfre=1;
//push
mpkv[key]=make_pair(value,1);
if(mpfre.find(1)==mpfre.end()){
mpfre[1].push_back(key);
mpiter[key]=--mpfre[1].end();
}
else{
mpiter[key] = mpfre[1].insert(mpfre[1].end(),key);
}
}
else{
mpkv[key].first=value;
get(key);
}
}
};
Design and implement a data structure for Least Recently Used (LRU) cache.
It should support the following operations:
getand
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 reached its capacity, it should invalidate the least recently used item before inserting a new item.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
LRUCache cache = new LRUCache( 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.put(4, 4); // evicts key 1 cache.get(1); // returns -1 (not found) cache.get(3); // returns 3 cache.get(4); // returns 4
题意:
实现一个LRU cache,最近最久未用淘汰算法,有两个操作get和put,get是访问,put是访问然后增加或者修改值,如果容量满了,淘汰最近最久未用的值。
可以使用list存储值,每次访问将先前的值删掉(如果存在),然后加入list的头。这样能保证尾部是最近最久未用的,删除的时候从尾部取就好了。因为要知道先前的位置,于是需要增加key对应的list中的位置。
注意:迭代器可以作为map的值,不能作为键。迭代器是引用,引用需初始化才能使用,不能直接比较大小。
代码:
class LRUCache
{
public:
LRUCache(int capacity)
{
maxSize = capacity;
}
int get(int key)
{
if (index.find(key) == index.end()) return -1;
int value=index[key]->second;
data.erase(index[key]);
data.push_front(make_pair(key,value));
index[key]=data.begin();
return value;
}
void put(int key, int value)
{
if (index.find(key) != index.end())
{
data.erase(index[key]);
data.push_front(make_pair(key,value));
index[key]=data.begin();
}
else
{
if (data.size() == maxSize)
{
index.erase(data.back().first);
data.pop_back();
}
data.push_front(make_pair(key,value));
index[key] = data.begin();
}
}
private:
int maxSize;
list<pair<int, int>> data;
unordered_map<int, list<pair<int, int>>::iterator> index;
};
460. LFU Cache
Design and implement a data structure for Least Frequently Used (LFU) cache. It
should support the following operations:
getand
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 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 cache,最近最不常用淘汰算法,有两个操作get和put,get是访问,put是访问然后增加或者修改值,如果容量满了,淘汰最近最不常用的值。
因为涉及频度fre,所以一个map存(key,<value,fre>),根据值能取到频度。
淘汰频度最小的,每个key可能在一个频度,再建一个键为fre的map存(fre,list<key>),这样知道删除哪个key。
因为要删除list中的元素,只知道key是无法删除自己的,需要知道迭代器,在建一个map存key到list<key>迭代器的映射。
代码:
class LFUCache {
public:
int size;
unordered_map<int,pair<int,int>> mpkv;
unordered_map<int,list<int>> mpfre;
unordered_map<int,list<int>::iterator> mpiter;
int minfre;
LFUCache(int capacity) {
size=capacity;
}
int get(int key) {
if(mpkv.find(key)==mpkv.end()) return -1;
int res=mpkv[key].first;
int fre=mpkv[key].second;
mpkv[key].second=fre+1;
mpfre[fre].erase(mpiter[key]);
if(fre==minfre&&mpfre[fre].size()==0) minfre++;
if(mpfre.find(fre+1)==mpfre.end()){
mpfre[fre+1].push_back(key);
mpiter[key]=--mpfre[fre+1].end();
}
else{
mpiter[key] = mpfre[fre+1].insert(mpfre[fre+1].end(),key);
}
return res;
}
void put(int key, int value) {
if (size <= 0) return;
if(mpkv.find(key)==mpkv.end()){
//pop
if(mpkv.size()==size){
int popkey=mpfre[minfre].front();
// cout<<"popkey:"<<popkey<<endl;
mpfre[minfre].pop_front();
mpkv.erase(popkey);
mpiter.erase(popkey);
}
// cout<<"pushkey:"<<key<<" val:"<<value<<endl;
minfre=1;
//push
mpkv[key]=make_pair(value,1);
if(mpfre.find(1)==mpfre.end()){
mpfre[1].push_back(key);
mpiter[key]=--mpfre[1].end();
}
else{
mpiter[key] = mpfre[1].insert(mpfre[1].end(),key);
}
}
else{
mpkv[key].first=value;
get(key);
}
}
};
相关文章推荐
- Leetcode 460. LFU Cache
- Leetcode 460. LFU Cache
- LeetCode 460. LFU Cache
- Leetcode 146. LRU Cache
- LeetCode——LRU Cache
- Leetcode——146. LRU Cache 一个优雅的LRU的C++实现
- Leetcode 146. LRU Cache (Hard) (cpp)
- LeetCode LRU Cache
- LeetCode-LRU Cache<未完成>
- leetcode 刷题:LRU cache
- leetcode 146. LRU Cache
- #leetcode#LRU Cache
- LRU Cache leetcode
- LeetCode "LRU Cache"
- leetcode LRU Cache
- Leetcode - 146. LRU Cache
- [LeetCode]146. LRU Cache
- Leetcode 146. LRU Cache
- LeetCode 146. LRU Cache
- LeetCode LRU Cache