forked from rui-yan/LeetCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-log-storage-system.py
40 lines (32 loc) · 1.02 KB
/
design-log-storage-system.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
# Time: put: O(1)
# retrieve: O(n + dlogd), n is the size of the total logs
# , d is the size of the found logs
# Space: O(n)
class LogSystem(object):
def __init__(self):
self.__logs = []
self.__granularity = {'Year': 4, 'Month': 7, 'Day': 10, \
'Hour': 13, 'Minute': 16, 'Second': 19}
def put(self, id, timestamp):
"""
:type id: int
:type timestamp: str
:rtype: void
"""
self.__logs.append((id, timestamp))
def retrieve(self, s, e, gra):
"""
:type s: str
:type e: str
:type gra: str
:rtype: List[int]
"""
i = self.__granularity[gra]
begin = s[:i]
end = e[:i]
return sorted(id for id, timestamp in self.__logs \
if begin <= timestamp[:i] <= end)
# Your LogSystem object will be instantiated and called as such:
# obj = LogSystem()
# obj.put(id,timestamp)
# param_2 = obj.retrieve(s,e,gra)