You are asked to design an in-memory key–value cache that supports a least-recently-used (LRU) eviction policy.
The cache must support the following operations:
get(key) -> value
:
key
exists in the cache, return its associated
value
and mark this entry as
most recently used
.
key
does not exist, return
-1
(or an equivalent sentinel value indicating a miss).
put(key, value) -> void
:
key
.
key
already exists, update its value and mark it as
most recently used
.
Requirements:
capacity > 0
.
get
and
put
operations must run in
amortized O(1) time
.
Example behavior (for illustration only):