Files
training-cg/lru_cache.py
T
2026-06-19 16:08:27 +02:00

27 lines
605 B
Python

class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.slots = []
def get(self, key: int) -> int:
if key <= len(self.slots) -1:
return self.slots[key]
return -1
def put(self, key: int, value: int) -> None:
if self.capacity > len(self.slots):
self.slots.append(value)
else:
self.slots[len(self.slots) - 1] = value
def main():
cache = LRUCache(2)
print(cache.get(1))
print(cache.get(0))
cache.put(0,1)
print(cache.get(0))
if __name__ == "__main__":
main()