-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathTime Based Key-Value Store.py
50 lines (37 loc) · 1.34 KB
/
Time Based Key-Value Store.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class TimeMap:
def __init__(self):
self.dict = {}
def set(self, key: str, value: str, timestamp: int) -> None:
if key not in self.dict:
self.dict[key] = ([], [])
self.dict[key][0].append(value)
self.dict[key][1].append(timestamp)
else:
self.dict[key][0].append(value)
self.dict[key][1].append(timestamp)
def bsearch(self, nums, target):
beg = 0
end = len(nums)-1
lastIndex = len(nums)-1
while beg<=end:
mid = (beg+end)//2
if target == nums[mid]:
return mid
elif target < nums[mid]:
end = mid-1
elif target > nums[mid]:
beg = mid+1
if target < nums[mid] and mid == 0:
return -1
if target > nums[mid]:
return mid
return mid-1
def get(self, key: str, timestamp: int) -> str:
if key not in self.dict:
return ""
index = self.bsearch(self.dict[key][1], timestamp)
return self.dict[key][0][index] if 0 <= index < len(self.dict[key][0]) else ""
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)