Design and implement a data structure for Least Recently Used (LRU) 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 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
Use a list<pair<int, int>> data to store the key-value pairs, unordered_map<int, list<pair<int, int>>::iterator> m to store the mapping from key to the corresponding iterator pointing into data.
The constructor LRUCache is trivial, just storing the capacity as member.
The get logic is:
- If the
keycannot be found inm, return-1. - Otherwise, move the key-value pair pointed by
m[key]to the front ofdata. And updatem[key]to point to the front ofdata. Return the valuedata.front().second.
The put logic is:
- Use
get(key)to test the existance of thekey.- If not found,
- Emit the last key-value pair, if the
datastorage is full. - Then insert the key-value pair to the front of
dataand updatem[key]accordingly.
- Emit the last key-value pair, if the
- Otherwise, just update
data.front().secondto bevalue.
- If not found,
// OJ: https://leetcode.com/problems/lru-cache
// Author: github.com/lzl124631x
// Time:
// get: O(1)
// put: O(1)
// Space: O(N)
class LRUCache {
private:
list<pair<int, int>> data;
unordered_map<int, list<pair<int, int>>::iterator> m;
int capacity;
public:
LRUCache(int capacity) : capacity(capacity) {}
int get(int key) {
if (!m.count(key)) return -1;
data.splice(data.begin(), data, m[key]);
m[key] = data.begin();
return data.front().second;
}
void put(int key, int value) {
if (get(key) == -1) {
if (data.size() == capacity) {
auto p = data.back();
m.erase(p.first);
data.pop_back();
}
data.emplace_front(key, value);
m[key] = data.begin();
} else data.front().second = value;
}
};