Problem of the day
1. Get(int num): If the key exists, it will return the value of the key stored. Else, return -1.
2. Put(int key, int value): If the key already exists, update the value of the key. Else add the key-value pair to the cache. If the number of keys is more than the capacity for this operation, delete the least recently key used.
For the following input:
4 2
2 1 4
1 1
1 4
We will initialize an empty LRU cache for the first operation with a maximum capacity of 2.
For the first operation, we need to add a key-value pair (1,4) to the cache.
For the second operation, we need to return the value stored for key 1, i.e., 4
For the third operation, we need to return -1, as we don't have any key 4 in the cache.
So, the final output will be:
4 -1
The first line of the input contains a single integer βTβ representing the no. of test cases.
The first line of each test case contains two integers βNβ and βXβ, denoting the no. of the operations. We need to initialize an empty LRU cache of the maximum capacity of βXβ.
The next βNβ lines of each test case contain either of the operations in the following format: -
Get => two space-separated integers, 1 and βYβ like '1 Y'. We need to return the value of the key βYβ if it exists. Otherwise, Return -1.
Put => three space-separated integers, 2, βAβ and βBβ like '2 A B'. If the key already exists, update the value of the key. Else add the key-value pair to the cache. If the number of keys is more than the capacity for this operation, delete the least recently key used.
For each test case, Return the results of the operations performed on the LRU separated by spaces.
You do not require to print anything. it has already been taken care of. Just implement the function and return the answer.
1 β€ T β€ 10
1 β€ N β€ 10^5
Ξ£N β€ 2*10^5
1 β€ X β€ (10^9)
1 β€ Y β€ (10^9)
1 β€ A β€ (10^9)
1 β€ B β€ (10^9)
Time Limit: 1 sec
2
3 2
2 1 4
1 1
1 4
4 1
2 1 4
2 2 6
1 1
1 2
##### Sample Output 1 :
4 -1
-1 6
For the first case:
For the following input:
3 2
2 1 4
1 1
1 4
We will initialize an empty LRU cache for the first operation with a maximum capacity of 2.
For the first operation, we need to add a key-value pair (1,4) to the cache.
For the second operation, we need to return the value stored for key 1, i.e., 4
For the third operation, we need to return -1, as we don't have any key 4 in the cache.
So, the final output will be:
4 -1
For the second case:
For the following input:
5 1
2 1 4
2 2 6
1 1
1 2
We will initialize an empty LRU cache for the first operation with a maximum capacity of 1.
For the first operation, we need to add a key-value pair (1,4) to the cache.
For the second operation, we need to add a key-value pair (2,6) to the cache and remove (1,4) from the cache.
For the third operation, we need to return -1, as we don't have any key 1 in the cache.
For the fourth operation, we need to return the value stored for key 2, i.e., 6
So, the final output will be:
-1 6
2
3 3
2 1 4
2 2 5
1 2
5 5
2 1 4
2 2 6
1 1
1 2
1 3
5
4 6 -1