-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.py
More file actions
37 lines (28 loc) · 1.29 KB
/
Copy path146.py
File metadata and controls
37 lines (28 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
int get(int key) Return the value of the key if the key exists, otherwise return -1.
void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
The functions get and put must each run in O(1) average time complexity."""
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.dict = {}
def get(self, key: int) -> int:
if key in self.dict:
val = self.dict.pop(key)
self.dict[key] = val
return self.dict[key]
else:
return -1
def put(self, key: int, value: int) -> None:
if key not in self.dict.keys():
if len(self.dict.keys()) == self.capacity:
del self.dict[next(iter(self.dict))]
else:
self.dict.pop(key)
self.dict[key] = value
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)