CODE-0003 · grok-composer-2.5-fast (default)from collections import OrderedDict
class LRUCache:
def __init__(self, capacity: int) -> None:
self._capacity = capacity
self._cache: OrderedDict[int, int] = OrderedDict()
def get(self, key: int) -> int:
if self._capacity == 0 or key not in self._cache:
return -1
self._cache.move_to_end(key)
return self._cache[key]
def put(self, key: int, value: int) -> None:
if self._capacity == 0:
return
if key in self._cache:
self._cache.move_to_end(key)
self._cache[key] = value
if len(self._cache) > self._capacity:
self._cache.popitem(last=False)