-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path981.py
32 lines (27 loc) · 881 Bytes
/
981.py
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
class TimeMap:
m = defaultdict(list)
def __init__(self):
self.m = defaultdict(list)
def findPos(self, key: str, timestamp: int) -> int:
values = self.m[key]
l, r = 0, len(values)-1
result = r
while l <= r and l < len(values) and r >= 0:
m = (l+r)//2
value, time = values[m]
if time > timestamp:
r = m-1
else:
result = m
l = m+1
return result
def set(self, key: str, value: str, timestamp: int) -> None:
self.m[key].append((value, timestamp))
def get(self, key: str, timestamp: int) -> str:
values = self.m[key]
if len(values) == 0:
return ""
value, time = values[self.findPos(key, timestamp)]
if time > timestamp:
return ""
return value