Python Solution with High Scalability


  • 0
    W
    from bisect import insort, bisect
    class LogSystem(object):
    
        def __init__(self):
            self.gmap={'Year':1,'Month':2,'Day':3,'Hour':4,'Minute':5,'Second':6}
            self.storage=[] # Instead of using dictionary, a binary search tree is used
    
        def put(self, id, timestamp):
            """
            :type id: int
            :type timestamp: str
            :rtype: void
            """
            t=tuple(timestamp.split(':')+[id]) # id is appended in the end
            insort(self.storage,t)
    
        def retrieve(self, s, e, gra):
            """
            :type s: str
            :type e: str
            :type gra: str
            :rtype: List[int]
            """
            ind=self.gmap[gra]
            s=tuple(s.split(':')[:ind]+['00']*(6-ind)+[float('-inf')]) # trick is here
            e=tuple(e.split(':')[:ind]+['99']*(6-ind)+[float('inf')])
    
            i=bisect(self.storage,s)
            j=bisect(self.storage,e)
    
            return [e[-1] for e in self.storage[i:j]]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.