-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-basedKey-value.py
35 lines (27 loc) · 948 Bytes
/
time-basedKey-value.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
33
34
35
class TimeMap:
def __init__(self):
self.timeDict = defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.timeDict[key].append([value, timestamp])
def get(self, key: str, timestamp: int) -> str:
timeList = self.timeDict[key]
if not timeList:
return ''
left = 0
right = len(timeList) - 1
while(left <= right):
mid = left + (right - left)//2
if(timeList[mid][1] == timestamp):
return timeList[mid][0]
elif(timeList[mid][1] < timestamp):
left = mid + 1
else:
right = mid - 1
if timeList[right][1] > timestamp:
return ''
else:
return timeList[right][0]
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)