Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import OrderedDict
- class LRUCache:
- def __init__(self, capacity):
- self.cache = OrderedDict()
- self.capacity = capacity
- def get(self, key):
- if key in self.cache:
- self.cache.move_to_end(key)
- return self.cache.get(key, -1)
- def put(self, key, value):
- if key in self.cache:
- self.cache.move_to_end(key)
- else:
- if(len(self.cache) >= self.capacity):
- self.cache.popitem(last=False)
- self.cache[key] = value
Add Comment
Please, Sign In to add comment