#K53982. Implement LRU Cache
Implement LRU Cache
Implement LRU Cache
You are required to implement a Least Recently Used (LRU) cache data structure. The cache supports the following three operations:
- SET key value: Insert or update the value for a given key. If the cache reaches its capacity, it should remove the least recently used item before inserting the new one.
- GET key: Retrieve the value associated with the key. If the key is not present in the cache, return -1. Note that a successful GET operation marks the key as recently used.
- DELETE key: Remove the key from the cache if it exists.
The cache must operate in accordance with the LRU strategy. For example, after a GET or an update via SET, the accessed key becomes the most recently used. The capacity \( C \) of the cache is provided in the input.
inputFormat
The input is read from standard input (stdin). The first line contains an integer representing the capacity ( C ) of the cache. The second line contains an integer ( N ) representing the number of operations to process. The next ( N ) lines each describe an operation in one of the following formats:
SET key value
GET key
DELETE key
For each GET
operation, you should output the result on a new line.
outputFormat
For each GET
operation, output the returned value (or -1 if the key isn't present in the cache) on a separate line to standard output (stdout).## sample
2
7
SET 1 1
SET 2 2
GET 1
SET 3 3
GET 1
GET 2
GET 3
1
1
-1
3
</p>