-
Notifications
You must be signed in to change notification settings - Fork 613
/
981.py
61 lines (46 loc) · 1.62 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
Create a timebased key-value store class TimeMap, that supports two operations.
1. set(string key, string value, int timestamp)
Stores the key and value, along with the given timestamp.
2. get(string key, int timestamp)
Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
If there are multiple such values, it returns the one with the largest timestamp_prev.
If there are no values, it returns the empty string ("").
'''
import bisect
class TimeMap(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.time_dict = {}
self.key_map = {}
def set(self, key, value, timestamp):
"""
:type key: str
:type value: str
:type timestamp: int
:rtype: None
"""
if key in self.time_dict:
self.time_dict[key].append(timestamp)
self.key_map[key].append(value)
else:
self.time_dict[key] = [timestamp]
self.key_map[key] = [value]
def get(self, key, timestamp):
"""
:type key: str
:type timestamp: int
:rtype: str
"""
if key in self.time_dict:
t_values = self.time_dict[key]
index = bisect.bisect_right(t_values, timestamp)
if index-1 == len(t_values) or index == 0:
return ''
return self.key_map[key][index-1]
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)