#K50962. LRU Cache Implementation
LRU Cache Implementation
LRU Cache Implementation
You are required to implement an LRU (Least Recently Used) cache that processes a series of operations. The cache supports the following two commands:
- SET k v: Set the key k with value v.
- GET k: Retrieve the value associated with key k.
If the cache reaches its maximum capacity, it should evict the least recently used item. The LRU eviction policy means that whenever a key is accessed (either a GET or an update via SET), it becomes the most recently used. Formally, if we denote the last access time of a key as \(t\), then the key with \(\min t\) is evicted when needed.
Your program must read from standard input and write to standard output.
inputFormat
The input begins with a line containing two space-separated integers: N and C, where N is the number of operations and C is the maximum capacity of the cache. Each of the following N lines contains an operation in one of the following forms: SET k v
or GET k
.
outputFormat
For each GET
operation, output the corresponding result on a new line. If the key is not found in the cache, output -1
.
7 2
SET 1 10
SET 2 20
GET 1
SET 3 30
GET 2
SET 4 40
GET 3
10
-1
30
</p>