leetcode - 362. Design Hit Counter

Description

Design a hit counter which counts the number of hits received in the past 5 minutes (i.e., the past 300 seconds).

Your system should accept a timestamp parameter (in seconds granularity), and you may assume that calls are being made to the system in chronological order (i.e., timestamp is monotonically increasing). Several hits may arrive roughly at the same time.

Implement the HitCounter class:

  • HitCounter() Initializes the object of the hit counter system.
  • void hit(int timestamp) Records a hit that happened at timestamp (in seconds). Several hits may happen at the same timestamp.
  • int getHits(int timestamp) Returns the number of hits in the past 5 minutes from timestamp (i.e., the past 300 seconds).

Example 1:

Input
["HitCounter", "hit", "hit", "hit", "getHits", "hit", "getHits", "getHits"]
[[], [1], [2], [3], [4], [300], [300], [301]]
Output
[null, null, null, null, 3, null, 4, 3]

Explanation
HitCounter hitCounter = new HitCounter();
hitCounter.hit(1);       // hit at timestamp 1.
hitCounter.hit(2);       // hit at timestamp 2.
hitCounter.hit(3);       // hit at timestamp 3.
hitCounter.getHits(4);   // get hits at timestamp 4, return 3.
hitCounter.hit(300);     // hit at timestamp 300.
hitCounter.getHits(300); // get hits at timestamp 300, return 4.
hitCounter.getHits(301); // get hits at timestamp 301, return 3.

Constraints:

1 <= timestamp <= 2 * 10^9
All the calls are being made to the system in chronological order (i.e., timestamp is monotonically increasing).
At most 300 calls will be made to hit and getHits.

Follow up: What if the number of hits per second could be huge? Does your design scale?

Solution

Prefix sum and binary search.

Use a list to store the (timestamp, hit_sum), and find the cur_timestamp - 300 in the list, subtract it from the current one, then we have the hits during a range.

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn), where n is the number of hit calls.
Space complexity: o ( n ) o(n) o(n)

Code

class HitCounter:

    def __init__(self):
        self.hit_sum = [(0, 0)]
        self.cur_hit = 0

    def hit(self, timestamp: int) -> None:
        self.cur_hit += 1
        self.hit_sum.append((timestamp, self.cur_hit))

    def _find_index(self, timestamp: int) -> int:
        left, right = 0, len(self.hit_sum) - 1
        while left < right:
            mid = (left + right + 1) >> 1
            if self.hit_sum[mid][0] > timestamp:
                right = mid - 1
            else:
                left = mid
        return (left + right + 1) >> 1

    def getHits(self, timestamp: int) -> int:
        index = self._find_index(timestamp - 300)
        return self.hit_sum[-1][1] - self.hit_sum[index][1]


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

相关推荐

  1. LeetCode 36

    2024-03-24 07:22:02       14 阅读
  2. leetcode - 362. Design Hit Counter

    2024-03-24 07:22:02       21 阅读
  3. leetcode-322. 零钱兑换

    2024-03-24 07:22:02       28 阅读
  4. leetcode 322.零钱兑换

    2024-03-24 07:22:02       20 阅读
  5. LEETCODE-DAY32

    2024-03-24 07:22:02       16 阅读
  6. LEETCODE-DAY36

    2024-03-24 07:22:02       13 阅读
  7. leetcode - 368. Largest Divisible Subset

    2024-03-24 07:22:02       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 07:22:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 07:22:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 07:22:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 07:22:02       20 阅读

热门阅读

  1. v-if 遇到 el-form 表单验证规则遇到的bug

    2024-03-24 07:22:02       19 阅读
  2. c语言:日期识别1

    2024-03-24 07:22:02       19 阅读
  3. 单片机MCU,MPU,SOC的工艺结构原理及选型参数总结

    2024-03-24 07:22:02       21 阅读
  4. TensorFlow打印网络参数的个数

    2024-03-24 07:22:02       18 阅读