-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path981.py
More file actions
35 lines (28 loc) · 869 Bytes
/
Copy path981.py
File metadata and controls
35 lines (28 loc) · 869 Bytes
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
from collections import defaultdict
class TimeMap:
def __init__(self):
self.map = defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.map[key].append([value, timestamp])
def get(self, key: str, timestamp: int) -> str:
recs = self.map[key]
res = ""
if recs == []:
return res
l, r = 0, len(recs) - 1
while l <= r:
mid = (l + r) // 2
if recs[mid][1] <= timestamp:
res = recs[mid][0]
l = mid + 1
else:
r = mid - 1
return res
timeMap = TimeMap()
timeMap.set("love", "high", 10)
timeMap.set("love", "low", 20)
print(timeMap.get("love", 5))
print(timeMap.get("love", 10))
print(timeMap.get("love", 15))
print(timeMap.get("love", 20))
print(timeMap.get("love", 25))