Leetcode 635 Solution

This article provides solution to leetcode question 635 (design-log-storage-system)

https://leetcode.com/problems/design-log-storage-system

Solution

class LogSystem:

    def __init__(self):
        self.logs = []

    def put(self, id: int, timestamp: str) -> None:
        self.logs.append((id, timestamp))

    def retrieve(self, start: str, end: str, granularity: str) -> List[int]:
        if granularity == 'Year':
            start = start[:5] + "01:01:00:00:00"
            end = end[:5] + "12:31:23:59:59"
        elif granularity == 'Month':
            start = start[:8] + "01:00:00:00"
            end = end[:8] + "31:23:59:59"
        elif granularity == 'Day':
            start = start[:11] + "00:00:00"
            end = end[:11] + "23:59:59"
        elif granularity == 'Hour':
            start = start[:14] + "00:00"
            end = end[:14] + "59:59"
        elif granularity == 'Minute':
            start = start[:17] + "00"
            end = end[:17] + "59"
        elif granularity == 'Second':
            pass
        else:
            raise Exception("Granularity {} not supported!".format(granularity))

        ans = []
        for log in self.logs:
            if start <= log[1] <= end:
                ans.append(log[0])
        return ans



# Your LogSystem object will be instantiated and called as such:
# obj = LogSystem()
# obj.put(id,timestamp)
# param_2 = obj.retrieve(start,end,granularity)