Design Hit Counter

Medium

Topics
QueueDesignBinary SearchData Stream

Design a hit counter that counts hits received in the past 5 minutes (300 seconds). Implement HitCounter(), hit(timestamp) which records a hit at the given timestamp (in seconds), and getHits(timestamp) which returns the number of hits in the past 300 seconds — that is, hits with a recorded time in the range (timestamp - 300, timestamp]. Calls are made in non-decreasing time order.

Example 1

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

Constraints

  • 1 <= timestamp <= 2*10^9
  • All calls are made with non-decreasing timestamps.
  • At most 300 calls per getHits window in the basic version.
Run ⌘' · Submit ⌘⏎