forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_097.py
47 lines (35 loc) · 1002 Bytes
/
problem_097.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
import bisect
class TimedMap:
def __init__(self):
self.map = dict()
def __repr__(self):
return str(self.map)
def setval(self, key, value, time):
if key not in self.map:
self.map[key] = ([time], [value])
return
times, values = self.map[key]
insertion_point = bisect.bisect(times, time)
times.insert(insertion_point, time)
values.insert(insertion_point, value)
def getval(self, key, time):
if key not in self.map:
return
times, values = self.map[key]
insertion_point = bisect.bisect(times, time)
if not insertion_point:
return
return values[insertion_point - 1]
d = TimedMap()
d.setval(1, 1, 0)
d.setval(1, 2, 2)
assert d.getval(1, 1) == 1
assert d.getval(1, 3) == 2
d = TimedMap()
d.setval(1, 1, 5)
assert not d.getval(1, 0)
assert d.getval(1, 10) == 1
d = TimedMap()
d.setval(1, 1, 0)
d.setval(1, 2, 0)
assert d.getval(1, 0) == 2