Design and implement a data structure for a Least Frequently Used (LFU) cache. Implement the LFUCache class: - LFUCache(int capacity) Initializes the object with the capacity of the data structure. - int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1. - void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, 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 used key would be invalidated. To determine the least frequently used key, a use counter is maintained for each key. The key with the smallest use counter is the least frequently used key. The functions get and put must each run in O(1) average time complexity.
Example 1
Input: operations = ["LFUCache","put","put","get","put","get","get","put","get","get","get"], arguments = [[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: After put(1), put(2) and get(1), key 1 has been used twice and key 2 once. put(3) evicts key 2, the least frequently used, so get(2) returns -1. get(3) raises key 3's use count to 2, tying key 1. put(4) evicts the less recently used of the two, key 1. So get(1) is -1 while keys 3 and 4 are found.
Example 2
Input: operations = ["LFUCache","get"], arguments = [[0],[0]]
Output: [null,-1]
Explanation: The cache has capacity 0 and holds nothing, so get(0) returns -1.
Example 3
Input: operations = ["LFUCache","put","get","put","get","get"], arguments = [[1],[1,1],[1],[2,2],[1],[2]]
Output: [null,null,1,null,-1,2]
Explanation: With capacity 1, get(1) finds key 1. put(2) must evict it to make room, so the next get(1) returns -1 and get(2) returns 2.
Group keys by their use count into buckets, each an LRU-ordered list. Track the minimum frequency so eviction and every access stay O(1).
Run checks all cases above. Submit evaluates all test cases.