Opening the reading…
Opening the reading…
LINKED LIST › LINKED LIST (PART 2) : DESIGN PATTERN
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →An LRU cache needs to answer two different questions at once. Given a key, it must find the value immediately. It must also know which key has been unused for the longest time when the cache is full. A single list answers the second question but searches too slowly; a hash map answers the first question but has no useful ordering.
Keep the keys in a doubly linked list ordered by recent use: the front is most recently used and the back is least recently used. Store each key's list iterator in a hash map. The map finds the node in average O(1), and the doubly linked list removes or inserts that node in O(1), because it already knows both neighboring links.
A successful get changes recency, so it removes the node and places it at the front. A put for an existing key does the same while replacing its value. A put for a new key inserts at the front; if that creates too many entries, the back node is the one that must be evicted.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) average | Each command performs a constant number of hash lookups and doubly linked list operations. Erasing, inserting at the front, and removing the back do not scan the cache, so no entry is processed repeatedly within one command. |
| Space | O(c) extra | The list and hash map store at most c cache entries, and the result array is required output and is excluded from the extra-space bound. The bound is O(min(c, n - 1)) for the actual commands, and reaches O(c) when enough distinct put commands fill the cache. |
#include <list>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> run(vector<string>& commands, vector<vector<int>>& args) {
int capacity = args[0][0];
list<pair<int, int>> order;
unordered_map<int, list<pair<int, int>>::iterator> position;
vector<int> result;
result.push_back(-2);
for (int i = 1; i < static_cast<int>(commands.size()); ++i) {
if (commands[i] == "get") {
int key = args[i][0];
auto found = position.find(key);
if (found == position.end()) {
result.push_back(-1);
continue;
}
auto node = found->second;
int value = node->second;
order.erase(node);
order.push_front({key, value});
position[key] = order.begin();
result.push_back(value);
} else {
int key = args[i][0];
int value = args[i][1];
auto found = position.find(key);
if (found != position.end()) {
order.erase(found->second);
} else if (static_cast<int>(order.size()) == capacity) {
auto last = order.back();
position.erase(last.first);
order.pop_back();
}
order.push_front({key, value});
position[key] = order.begin();
result.push_back(-2);
}
}
return result;
}
};The map entry is deliberately replaced after every move. Erasing a list node invalidates the iterator that pointed to it, so leaving the old iterator in the map would make the next operation use an invalid location. The list is the ownership of recency; the map is only the fast route to the current list node.