[
design
hash table
]
Leetcode 0635 Design Log Storage System
Problem statement
https://leetcode.com/problems/design-log-storage-system/
Solution
The main difficulty is to parse our string with granularity. For example if we have day granularity, we need to compare only starts of our strings: 2017:01:01.
Complexity
Time complexity is O(1) for Put and O(n) for Retrieve, because we are checking all elements. We can also use binary search for search, if we use SortedList, but complexity will be still O(n) (it can be O(log n), if we need to return number of logs, but not all of them).
Code
class LogSystem:
def __init__(self):
self.logs = []
def put(self, id, timestamp):
self.logs.append((timestamp, id))
def retrieve(self, s, e, gra):
ind = {"Year":4, "Month":7, "Day":10, "Hour":13, "Minute":16, "Second":19}[gra]
beg, end = s[:ind], e[:ind]
return [id for time, id in self.logs if beg <= time[:ind] <= end]