Problem
Section titled “Problem”Design and implement a data structure for a Least Frequently Used (LFU) cache.
Implement the LFUCache class:
LFUCache(int capacity)Initializes the object with thecapacityof the data structure.int get(int key)Gets the value of thekeyif thekeyexists in the cache. Otherwise, returns-1.void put(int key, int value)Update the value of thekeyif present, or inserts thekeyif not already present. When the cache reaches itscapacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently usedkeywould be invalidated.
To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.
When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.
The functions get and put must each run in average time complexity.
Example 1:
Input["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"][[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]Output[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
Explanation// cnt(x) = the use counter for key x// cache=[] will show the last used order for tiebreakers (leftmost element is most recent)LFUCache lfu = new LFUCache(2);lfu.put(1, 1); // cache=[1,_], cnt(1)=1lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1lfu.get(1); // return 1 // cache=[1,2], cnt(2)=1, cnt(1)=2lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2. // cache=[3,1], cnt(3)=1, cnt(1)=2lfu.get(2); // return -1 (not found)lfu.get(3); // return 3 // cache=[3,1], cnt(3)=2, cnt(1)=2lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1. // cache=[4,3], cnt(4)=1, cnt(3)=2lfu.get(1); // return -1 (not found)lfu.get(3); // return 3 // cache=[3,4], cnt(4)=1, cnt(3)=3lfu.get(4); // return 4 // cache=[4,3], cnt(4)=2, cnt(3)=3Constraints:
-
capacity -
key -
value - At most calls will be made to
getandput.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”On eviction we need “the least recently used key among those with the smallest use counter”. A heap or an ordered map keyed by (freq, time) finds it in , but the problem demands . Three observations get us there:
-
Bucket keys by frequency. For every frequency
fkeep a list of the keys that currently have counterf, ordered by recency (most recent at the front). Within one bucket the LRU key is simply the back of the list. -
Counters only move by one. A
getorputon an existing key moves it from bucketfto the front of bucketf + 1. With a key-to-node map, that is an unlink plus an insert (list::splicedoes both without reallocating). -
The minimum frequency is easy to maintain. Keep
minFreq. It only changes in two ways:- a new key is inserted with counter
1, sominFreq = 1; - a key is promoted out of bucket
minFreqand leaves it empty, sominFreqbecomesminFreq + 1(the promoted key is now there).
Eviction never needs to search for a new minimum, because it is always immediately followed by an insert that sets
minFreq = 1. - a new key is inserted with counter
So the victim is always byFreq[minFreq].back(), found in .
Algorithm
Section titled “Algorithm”State: capacity, minFreq, byFreq (frequency → list<Entry>, most recent first) and where (key → iterator to its Entry, which stores key, value, freq).
touch(it)— promote an entry: splice it out ofbyFreq[f]and onto the front ofbyFreq[f + 1]and setfreq = f + 1. IfbyFreq[f]became empty, erase it, and iff == minFreq, incrementminFreq.get(key): ifkeyis not inwhere, return-1. Otherwisetouchits entry and return itsvalue.put(key, value):- If
keyexists, overwrite itsvalueandtouchit. - Otherwise, if the cache is full (
where.size() == capacity), evictbyFreq[minFreq].back(): erase its key fromwhere, pop it, and erase the bucket if it became empty. - Insert
{key, value, 1}at the front ofbyFreq[1], record it inwhere, and setminFreq = 1.
- If
Complexity
Section titled “Complexity”- Time: average per
getandput— a constant number of hash-map lookups and list splices/pushes/pops. - Space: — one entry per cached key, and at most one non-empty bucket per distinct frequency among them.
Edge cases
Section titled “Edge cases”- Tie on frequency (the
put(4, 4)step above): bucket2holds[3, 1];1was used less recently, so it is at the back and is evicted. puton an existing key counts as a use: the value is overwritten and the key is promoted, and no eviction happens.- Capacity
1: every insert of a new key evicts the only key;minFreqis then reset to1by the insert.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class LFUCache { struct Entry { int key, value, freq; };
int capacity, minFreq = 0; unordered_map<int, list<Entry>> byFreq; // freq -> entries, most recent first unordered_map<int, list<Entry>::iterator> where; // key -> its entry
// Count one more use: move the entry to the front of the next bucket. void touch(list<Entry>::iterator it) { int f = it->freq; list<Entry>& from = byFreq[f]; list<Entry>& to = byFreq[f + 1]; to.splice(to.begin(), from, it); it->freq = f + 1; if (from.empty()) { byFreq.erase(f); if (minFreq == f) minFreq = f + 1; } }
public: LFUCache(int capacity) : capacity(capacity) {}
int get(int key) { auto found = where.find(key); if (found == where.end()) return -1; touch(found->second); return found->second->value; }
void put(int key, int value) { auto found = where.find(key); if (found != where.end()) { found->second->value = value; touch(found->second); return; } if ((int)where.size() == capacity) { list<Entry>& bucket = byFreq[minFreq]; where.erase(bucket.back().key); bucket.pop_back(); if (bucket.empty()) byFreq.erase(minFreq); } list<Entry>& ones = byFreq[1]; ones.push_front({key, value, 1}); where[key] = ones.begin(); minFreq = 1; }};class LFUCache { private final int capacity; private int minFreq = 0; private final HashMap<Integer, Integer> values = new HashMap<>(); // key -> value private final HashMap<Integer, Integer> freqOf = new HashMap<>(); // key -> use count // freq -> keys, least recent first (LinkedHashSet keeps insertion order) private final HashMap<Integer, LinkedHashSet<Integer>> byFreq = new HashMap<>();
public LFUCache(int capacity) { this.capacity = capacity; }
// Count one more use: move the key to the most-recent end of the next bucket. private void touch(int key) { int f = freqOf.get(key); LinkedHashSet<Integer> from = byFreq.get(f); from.remove(key); if (from.isEmpty()) { byFreq.remove(f); if (minFreq == f) minFreq = f + 1; } byFreq.computeIfAbsent(f + 1, x -> new LinkedHashSet<>()).add(key); freqOf.put(key, f + 1); }
public int get(int key) { Integer value = values.get(key); if (value == null) return -1; touch(key); return value; }
public void put(int key, int value) { if (values.containsKey(key)) { values.put(key, value); touch(key); return; } if (values.size() == capacity) { LinkedHashSet<Integer> bucket = byFreq.get(minFreq); int victim = bucket.iterator().next(); bucket.remove(victim); if (bucket.isEmpty()) byFreq.remove(minFreq); values.remove(victim); freqOf.remove(victim); } values.put(key, value); freqOf.put(key, 1); byFreq.computeIfAbsent(1, x -> new LinkedHashSet<>()).add(key); minFreq = 1; }}from collections import OrderedDict, defaultdict
class LFUCache:
def __init__(self, capacity: int): self.capacity = capacity self.min_freq = 0 self.values = {} # key -> value self.freq = {} # key -> use count # freq -> keys, least recent first self.by_freq = defaultdict(OrderedDict)
def _touch(self, key: int) -> None: # Count one more use: move the key to the most-recent end of the next bucket. f = self.freq[key] bucket = self.by_freq[f] del bucket[key] if not bucket: del self.by_freq[f] if self.min_freq == f: self.min_freq = f + 1 self.by_freq[f + 1][key] = None self.freq[key] = f + 1
def get(self, key: int) -> int: if key not in self.values: return -1 self._touch(key) return self.values[key]
def put(self, key: int, value: int) -> None: if key in self.values: self.values[key] = value self._touch(key) return if len(self.values) == self.capacity: bucket = self.by_freq[self.min_freq] victim, _ = bucket.popitem(last=False) if not bucket: del self.by_freq[self.min_freq] del self.values[victim] del self.freq[victim] self.values[key] = value self.freq[key] = 1 self.by_freq[1][key] = None self.min_freq = 1Give it a try first
Attempt the problem on LeetCode — the code will still be here.
