
Problem
Solution
Submissions
LFU Cache
Certification: Advanced Level
Accuracy: 100%
Submissions: 1
Points: 15
Design and implement a data structure for a Least Frequently Used (LFU) cache. The LFU cache should support the following operations: get and put.
- get(key): Get the value of the key if it exists in the cache, otherwise return -1.
- put(key, value): Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item 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 used key would be evicted.
The cache must be implemented with a time complexity of O(1) for both operations.
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:
LFUCache lfu = new LFUCache(2); // Cache capacity is 2
lfu.put(1, 1); // cache is {1=1}
lfu.put(2, 2); // cache is {1=1, 2=2}
lfu.get(1); // return 1, cache is now {1=1, 2=2} with frequency of key 1 increased
lfu.put(3, 3); // key 2 is evicted since it has lower frequency, cache is {1=1, 3=3}
lfu.get(2); // returns -1 (not found)
lfu.get(3); // returns 3, cache is {1=1, 3=3} with frequency of key 3 increased
lfu.put(4, 4); // key 1 is evicted since both keys have same frequency but 1 is less recently used, cache is {4=4, 3=3}
lfu.get(1); // returns -1 (not found)
lfu.get(3); // returns 3, cache is {4=4, 3=3} with frequency of key 3 increased
lfu.get(4); // returns 4, cache is {4=4, 3=3} with frequency of key 4 increased
Example 2
- Input: ["LFUCache", "put", "put", "put", "get", "get"]
[[2], [1, 1], [2, 2], [1, 3], [1], [2]] - Output: [null, null, null, null, 3, 2]
- Explanation:
LFUCache lfu = new LFUCache(2); // Cache capacity is 2
lfu.put(1, 1); // cache is {1=1}
lfu.put(2, 2); // cache is {1=1, 2=2}
lfu.put(1, 3); // update the value of key 1, cache is {1=3, 2=2}
lfu.get(1); // returns 3
lfu.get(2); // returns 2
Constraints
- 0 <= capacity <= 10^4
- 0 <= key <= 10^5
- 0 <= value <= 10^9
- At most 2 * 10^5 calls will be made to get and put
- Time Complexity: O(1) for both get and put operations
- Space Complexity: O(capacity)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a HashMap to store the key-value pairs for O(1) lookup
- Use a second HashMap to keep track of the frequency of each key
- Group keys with the same frequency together using a HashMap of LinkedHashSets
- Use a LinkedHashSet for each frequency to maintain the order of insertion (for tie-breaking)
- Keep track of the minimum frequency for easy removal when the cache is full
- Update the frequency of a key whenever it's accessed or updated
- When evicting, remove the least recently used key with the minimum frequency