LFU Cache
O(1) cache evicting the least frequently used key; ties broken by least recent.
Open on LeetCode ↗Intuition
Group keys by use-count: freq → an ordered set of keys (insertion order = recency). Track the minimum frequency. A touch moves a key from bucket f to f+1; eviction pops the oldest key from the min-freq bucket. Every step is a dict/OrderedDict operation — O(1).
Approach
Two maps + a counter
vals key→(value,freq); buckets freq→OrderedDict of keys; minf = current lowest frequency.
Touch promotes
Remove key from bucket f; add to bucket f+1. If bucket f was the min and is now empty, minf += 1 (the only way minf rises).
Insert resets minf
A new key has freq 1 → minf = 1. Eviction (before insert, when full): popitem(last=False) on bucket minf = oldest of the least-used.
Solution & live demo
Edge cases
Value update counts as a use — promote, don't evict.
Every put is a no-op; guard first.