
You are given a capacity ‘C’ for LRUCache(int capacity) which basically initializes the LRU cache with positive size capacity.
Then you are given a ‘KEY’ for which the function int get(int key) returns the value of the key if the key exists, otherwise, return -1.
You are given two integers ‘KEY’ and ‘VALUE’ for which the function void put(int key, int value) updates the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains the ‘Q’ i.e. number of queries.
The first integer of each query defines the type of queries that can contain values ‘0’,’1’, or ‘2’.
If the value is ‘0’, it denotes that constructor ‘LRUCache’ is called.
If the value is ‘1’, it denotes that function ‘get’ is called.
If the value is ‘2’, it denotes that function ‘put’ is called.
Now,
If the query is ‘0’, then it follows by one integer, for the capacity size.
If the query is ‘1’, then it follows by one integer, for the key.
If the query is ‘0’, then it follows by two integers, for the key and value pair.
Each query is in a different line.
For each test case, print the output array/list.
You do not need to print anything; it has already been taken care of.
1 <= T <= 50
1 <= N <= 10^4
0 <= QType[i] <= 2
1 <= QValues[i] <= 10^5
Where ‘T’ is the number of test cases.
'N' is the given capacity size of the LRU Cache.
‘QType’ is the given type of query.
‘QValues’ is the given query values.
Time Limit: 1 sec.
Let's try assuming that you never had to remove entries from the LRU cache because we had enough space, what would you use to support and get and put functions? A simple HashMap would be sufficient in that case.
Now, look at the second part which is where the eviction comes in, here we need a data structure which at any given instance can give us the least recently used objects in order.
Let’s see how the functions would work :
Reverse The Array
Reverse The Array
Reverse The Array
Temporary Tree
LFU Cache
LFU Cache
LFU Cache
LFU Cache
LFU Cache
LFU Cache
Set Matrix Zeros
Set Matrix Zeros
Set Matrix Zeros
Set Matrix Zeros
Set Matrix Zeros
Set Matrix Zeros
Missing Number