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 LFU cache evicts the key with the smallest access frequency. If several keys have that frequency, it evicts the least recently used one. The eviction rule therefore has two independent parts: group keys by frequency, then keep each group ordered from most recent to least recent.
A hash map can find a key's value and current frequency in average O(1) time, but it cannot by itself remove the oldest key from one frequency group in O(1). A doubly linked list solves that part: put recently used keys at the front, keep the eviction candidate at the back, and store an iterator for every key so moving it between lists is constant time.
The remaining question is how to find the smallest nonempty frequency without scanning all buckets. Keep minFreq updated whenever a key moves away from its old bucket, and reset it to 1 whenever a new key enters. The cache can then evict from the back of the list at minFreq immediately.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(1) average per operation, O(n) total | Each operation performs a constant number of hash-table lookups and linked-list insertions, erasures, or front/back accesses. Hash-table operations are O(1) average, and no list is scanned; over n operations, the total is O(n) average. |
| Space | O(C) extra | At most C keys occupy the cache, and each key appears in one value entry, one frequency entry, one iterator entry, and one list node. The returned result vector is required output and is excluded. Empty frequency buckets are erased, so even an input where one key reaches many frequencies does not leave O(n) unused lists behind. |
#include <list>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> lfuCache(int capacity, vector<string>& ops, vector<vector<int>>& args) {
vector<string> result;
if (capacity == 0) {
for (int i = 0; i < static_cast<int>(ops.size()); ++i) {
result.push_back("null");
}
return result;
}
unordered_map<int, int> keyToVal;
unordered_map<int, int> keyToFreq;
unordered_map<int, list<int>::iterator> keyToIter;
unordered_map<int, list<int>> freqToList;
int minFreq = 0;
for (int i = 0; i < static_cast<int>(ops.size()); ++i) {
if (ops[i] == "LFUCache") {
result.push_back("null");
} else if (ops[i] == "get") {
int key = args[i][0];
auto valueIt = keyToVal.find(key);
if (valueIt == keyToVal.end()) {
result.push_back("-1");
continue;
}
result.push_back(to_string(valueIt->second));
int freq = keyToFreq[key];
auto& oldList = freqToList[freq];
oldList.erase(keyToIter[key]);
if (oldList.empty()) {
if (freq == minFreq) {
++minFreq;
}
freqToList.erase(freq);
}
++freq;
keyToFreq[key] = freq;
freqToList[freq].push_front(key);
keyToIter[key] = freqToList[freq].begin();
} else if (ops[i] == "put") {
int key = args[i][0];
int value = args[i][1];
auto valueIt = keyToVal.find(key);
if (valueIt != keyToVal.end()) {
valueIt->second = value;
int freq = keyToFreq[key];
auto& oldList = freqToList[freq];
oldList.erase(keyToIter[key]);
if (oldList.empty()) {
if (freq == minFreq) {
++minFreq;
}
freqToList.erase(freq);
}
++freq;
keyToFreq[key] = freq;
freqToList[freq].push_front(key);
keyToIter[key] = freqToList[freq].begin();
} else {
if (static_cast<int>(keyToVal.size()) == capacity) {
int evictKey = freqToList[minFreq].back();
freqToList[minFreq].pop_back();
if (freqToList[minFreq].empty()) {
freqToList.erase(minFreq);
}
keyToVal.erase(evictKey);
keyToFreq.erase(evictKey);
keyToIter.erase(evictKey);
}
keyToVal[key] = value;
keyToFreq[key] = 1;
freqToList[1].push_front(key);
keyToIter[key] = freqToList[1].begin();
minFreq = 1;
}
result.push_back("null");
}
}
return result;
}
};