Skip to content

Latest commit

 

History

History
80 lines (54 loc) · 1.98 KB

_362. Design Hit Counter.md

File metadata and controls

80 lines (54 loc) · 1.98 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : October 24, 2024

Last updated : October 24, 2024


Related Topics : Array, Binary Search, Design, Queue, Data Stream

Acceptance Rate : 69.02 %


Solutions

Python

class HitCounter:

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

    def hit(self, timestamp: int) -> None:
        self.hits.append(timestamp)

    def getHits(self, timestamp: int) -> int:
        return bisect_right(self.hits, timestamp + 299) - bisect_left(self.hits, timestamp - 299)


# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)
class HitCounter:

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

    def hit(self, timestamp: int) -> None:
        self.hits.append(timestamp)

    def getHits(self, timestamp: int) -> int:
        indx = bisect_left(self.hits, timestamp)
        cnt = 0

        for i in range(min(indx, len(self.hits) - 1), -1, -1) :
            if abs(self.hits[i] - timestamp) >= 300 :
                break
            cnt += 1
        
        for i in range(indx + 1, len(self.hits)) :
            if abs(self.hits[i] - timestamp) >= 300 :
                break
            cnt += 1
        
        return cnt


# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)