← 返回 anthropic 的题目列表LRU Cache Extension
类型:online_judge
Given an already implemented in-memory cache (LRU Cache), you are required to extend its basic functionalities. Design and implement a class to perform these extensions. The required basic functionalities include get(key) and put(key, value). You need to add extended functionalities on top of these and provide test cases. The capacity of the cache capacity is provided during initialization, and you must implement the data eviction strategy when it exceeds the capacity.
Example
# Assume the capacity is 2
Cache operations:
cache.put(1, 1)
cache.put(2, 2)
cache.get(1) # returns 1
cache.put(3, 3) # This operation will make the key 2 invalid
cache.get(2) # returns -1 (not found)
cache.put(4, 4) # This operation will make the key 1 invalid
cache.get(1) # returns -1 (not found)
cache.get(3) # returns 3
cache.get(4) # returns 4
Data Scale
key and value are integers.
Up to 10,000 calls to get and put operations.
1 <= capacity <= 1000.
Example
Input
2
put 1 1
put 2 2
get 1
put 3 3
get 2
put 4 4
get 1
get 3
get 4