So far easiest and clean Python solution. Beats 100%. 4 Liner


  • 0
    W

    '''

    from collections import deque
    class HitCounter(object):
    
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.hits = deque()
    
    
        def hit(self, timestamp):
            """
            Record a hit.
            @param timestamp - The current timestamp (in seconds granularity).
            :type timestamp: int
            :rtype: void
            """
            self.hits.append(timestamp)
    
    
        def getHits(self, timestamp):
            """
            Return the number of hits in the past 5 minutes.
            @param timestamp - The current timestamp (in seconds granularity).
            :type timestamp: int
            :rtype: int
            """
            while self.hits and timestamp - self.hits[0] >= 300:
                self.hits.popleft()
            return len(self.hits)
    

    '''


Log in to reply
 

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