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 attimestamp
(in seconds). Several hits may happen at the sametimestamp
.int getHits(int timestamp)
Returns the number of hits in the past 5 minutes fromtimestamp
(i.e., the past300
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 * 109
- 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 tohit
andgetHits
.
Follow up: What if the number of hits per second could be huge? Does your design scale?
Solutionsο
Solution 1: Binary Searchο
Since timestamp
is monotonically increasing, we can use an array ts
to store all timestamp
s. Then in the getHits
method, we use binary search to find the first position that is greater than or equal to timestamp - 300 + 1
, and then return the length of ts
minus this position.
In terms of time complexity, the time complexity of the hit
method is $O(1)$, and the time complexity of the getHits
method is $O(\log n)$. Where $n$ is the length of ts
.
Python3ο
class HitCounter:
def __init__(self):
self.ts = []
def hit(self, timestamp: int) -> None:
self.ts.append(timestamp)
def getHits(self, timestamp: int) -> int:
return len(self.ts) - bisect_left(self.ts, timestamp - 300 + 1)
# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)
Javaο
class HitCounter {
private List<Integer> ts = new ArrayList<>();
public HitCounter() {
}
public void hit(int timestamp) {
ts.add(timestamp);
}
public int getHits(int timestamp) {
int l = search(timestamp - 300 + 1);
return ts.size() - l;
}
private int search(int x) {
int l = 0, r = ts.size();
while (l < r) {
int mid = (l + r) >> 1;
if (ts.get(mid) >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/
C++ο
class HitCounter {
public:
HitCounter() {
}
void hit(int timestamp) {
ts.push_back(timestamp);
}
int getHits(int timestamp) {
return ts.end() - lower_bound(ts.begin(), ts.end(), timestamp - 300 + 1);
}
private:
vector<int> ts;
};
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter* obj = new HitCounter();
* obj->hit(timestamp);
* int param_2 = obj->getHits(timestamp);
*/
Goο
type HitCounter struct {
ts []int
}
func Constructor() HitCounter {
return HitCounter{}
}
func (this *HitCounter) Hit(timestamp int) {
this.ts = append(this.ts, timestamp)
}
func (this *HitCounter) GetHits(timestamp int) int {
return len(this.ts) - sort.SearchInts(this.ts, timestamp-300+1)
}
/**
* Your HitCounter object will be instantiated and called as such:
* obj := Constructor();
* obj.Hit(timestamp);
* param_2 := obj.GetHits(timestamp);
*/
TypeScriptο
class HitCounter {
private ts: number[] = [];
constructor() {}
hit(timestamp: number): void {
this.ts.push(timestamp);
}
getHits(timestamp: number): number {
const search = (x: number) => {
let [l, r] = [0, this.ts.length];
while (l < r) {
const mid = (l + r) >> 1;
if (this.ts[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
return this.ts.length - search(timestamp - 300 + 1);
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* var obj = new HitCounter()
* obj.hit(timestamp)
* var param_2 = obj.getHits(timestamp)
*/
Rustο
struct HitCounter {
ts: Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl HitCounter {
fn new() -> Self {
HitCounter { ts: Vec::new() }
}
fn hit(&mut self, timestamp: i32) {
self.ts.push(timestamp);
}
fn get_hits(&self, timestamp: i32) -> i32 {
let l = self.search(timestamp - 300 + 1);
(self.ts.len() - l) as i32
}
fn search(&self, x: i32) -> usize {
let (mut l, mut r) = (0, self.ts.len());
while l < r {
let mid = (l + r) / 2;
if self.ts[mid] >= x {
r = mid;
} else {
l = mid + 1;
}
}
l
}
}