I was doing an exercise in an online judge:
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(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.
I basically use std::list and std::unordered_map and works good in small input case. But OJ give a Time Limit Exceeded on the input: cache size is 2048 and 20000+ get & set operations.
Time Limit Exceeded version:
class LRUCache {
public:
LRUCache(int capacity):cacheSize(capacity) {
}
int get(int key) {
auto it = mapping.find(key);
if(it == mapping.end())
return -1;
itemList.splice(itemList.begin(),itemList,it->second);
//mapping[key] == it->second still holds
return it->second->second;
}
void set(int key, int value) {
auto it = mapping.find(key);
if(it != mapping.end()) {
itemList.splice(itemList.begin(),itemList,it->second);
it->second->second = value;
} else {
itemList.push_front(make_pair(key,value));
mapping.insert(make_pair(key,itemList.begin()));
}
if(itemList.size() > cacheSize) {
mapping.erase(itemList.back().first);
itemList.pop_back();
}
}
private:
int cacheSize;
list<pair<int,int> > itemList;
unordered_map<int,list<pair<int,int> >::iterator> mapping;
};
Then I thought why not erase element before insert one, so I modify set function and OJ accept!
Accept version:
void set(int key, int value) {
auto it = mapping.find(key);
if(it != mapping.end()) {
itemList.splice(itemList.begin(),itemList,it->second);
it->second->second = value;
} else {
if(itemList.size() == cacheSize) {
mapping.erase(itemList.back().first);
itemList.pop_back();
}
itemList.push_front(make_pair(key,value));
mapping.insert(make_pair(key,itemList.begin()));
}
}
I wonder what makes such a different?