1from collections import OrderedDict
2
3class LRUCache:
4
5 def __init__(self, capacity: int):
6 self.d = OrderedDict()
7 self.c = capacity
8
9 def get(self, key: int) -> int:
10 if key in self.d:
11 self.d.move_to_end(key, last=True)
12 return self.d[key]
13 return -1
14
15 def put(self, key: int, value: int) -> None:
16 if key in self.d:
17 self.d[key] = value
18 elif len(self.d) == self.c:
19 self.d.popitem(last=False)
20 self.d[key] = value
21 self.d.move_to_end(key, last=True)
22
23
24# Your LRUCache object will be instantiated and called as such:
25# obj = LRUCache(capacity)
26# param_1 = obj.get(key)
27# obj.put(key,value)