Practice LFU Cache
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
LFUCache(capacity) stores key-value entries. get(key) returns the value or -1 if absent. put(key,value) inserts or updates. New entries start with frequency one; a successful get or update increases frequency. When insertion needs space, evict the least-frequent key, breaking ties by least-recent use. Both operations require average O(1) time. Practice approach: Map each key to its value, frequency, and linked-list node. Maintain a recency list for each frequency and track the minimum nonempty frequency. Move accessed entries between adjacent frequency lists. Test frequency ties, updates, and eviction immediately after a read.
Source: LFU Cache on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["LFUCache","put","put","get","put","get","get","put","get","get"] [[2],[4,40],[7,70],[4],[9,90],[7],[9],[8,80],[4],[8]]
[null,null,null,40,null,-1,90,null,-1,80]
Reading 4 protects it from the first eviction. After 9 is read, frequencies tie; inserting 8 evicts the less-recently used 4.
Constraints
Related LeetCode Problems
Practice more problems similar to LFU Cache.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.