Python solution..need help


  • 0
    A

    I can't seem to figure out, but for some reason my program is failing at this test case when I try to submit. But when I run the test individually, I get back the expected result. Any help is appreciated.

    Test case:
    ["HitCounter","hit","hit","hit","getHits","hit","getHits"]
    [[],[1],[2],[3],[4],[300],[301]]

    
    class HitCounter(object):
    
        tsList=[]
    
        def __init__(self):
            """
            Initialize your data structure here.
            """
            for i in range(300):
                self.tsList.append(None)
            
            
    
        def hit(self, timestamp):
            """
            Record a hit.
            @param timestamp - The current timestamp (in seconds granularity).
            :type timestamp: int
            :rtype: void
            """
            index = timestamp
            while self.tsList[index%300]== timestamp and (index-timestamp) <=300:
                index+=1
            
            self.tsList[index%300] = 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
            """
            hits=0
            fiveMinsOld = timestamp - 300
            for i in range(300):
                if self.tsList[i]>fiveMinsOld:
                    hits+=1
            return hits
    
    # Your HitCounter object will be instantiated and called as such:
    # obj = HitCounter()
    # obj.hit(timestamp)
    # param_2 = obj.getHits(timestamp)
    
    

Log in to reply
 

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