class HitCounter {
public:
HitCounter() {
}
void hit(int timestamp) {
}
int getHits(int timestamp) {
}
};
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter* obj = new HitCounter();
* obj->hit(timestamp);
* int param_2 = obj->getHits(timestamp);
*/
class HitCounter:
# 方法1: 哈希字典
def __init__(self):
"""
Initialize your data structure here.
"""
self.time_freq = defaultdict(int)
def hit(self, timestamp: int) -> None:
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
"""
self.time_freq[timestamp] += 1
def getHits(self, timestamp: int) -> int:
"""
Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity).
"""
res = 0
k = list(self.time_freq.keys())
for key in k:
if timestamp - key < 300:
res += self.time_freq[key]
else:
del self.time_freq[key]
return res
class HitCounter2:
# 方法2:数组+二分查找
def __init__(self):
"""
Initialize your data structure here.
"""
self.nums = []
def hit(self, timestamp: int) -> None:
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
"""
self.nums.append(timestamp)
def getHits(self, timestamp: int) -> int:
"""
Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity).
"""
res = 0
window_L_val = timestamp - 299
window_L = bisect.bisect_left(self.nums, window_L_val)
#借助二分查找
return len(self.nums) - window_L
# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)
class HitCounter {
int[] times = new int[300];
int[] hits = new int[300];
public HitCounter() {
}
public void hit(int timestamp) {
int index = timestamp % 300;
if (times[index] != timestamp) {
times[index] = timestamp;
hits[index] = 1;
} else {
hits[index]++;
}
}
public int getHits(int timestamp) {
int res = 0;
for (int i = 0; i < 300; i++) {
if (timestamp - times[i] < 300) {
res += hits[i];
}
}
return res;
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/
class HitCounter {
/** Initialize your data structure here. */
Queue<Integer> q;
int count;
public HitCounter() {
q = new LinkedList<Integer>();
count = 0;
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
q.add(timestamp);
count++;
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
if(q.size() == 0) return 0;
int peek = q.peek();
while(peek < timestamp - 299) {
q.poll();
count--;
if(q.isEmpty()) return 0;
peek = q.peek();
}
return count;
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/
class HitCounter1 {
public:
/** Initialize your data structure here. */
HitCounter1() {
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
void hit(int timestamp) {
time2Cnt[timestamp]++;
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
int getHits(int timestamp) {
int count = 0;
for (int i = timestamp - 300 + 1; i <= timestamp; i++) {
count += time2Cnt[i];
}
return count;
}
private:
unordered_map<int, int> time2Cnt;
};
class HitCounter2 {
public:
/** Initialize your data structure here. */
HitCounter2() {
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
void hit(int timestamp) {
q.push(timestamp);
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
int getHits(int timestamp) {
while(!q.empty() && timestamp - q.front() >= 300) {
q.pop();
}
return q.size();
}
private:
queue<int> q;
};
class HitCounter {
public:
/** Initialize your data structure here. */
HitCounter() {
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
void hit(int timestamp) {
if (!dq.empty() && dq.back().first == timestamp) {
dq.back().second++; // could have many hits in one second
} else {
dq.push_back({timestamp, 1});
}
cnt++;
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
int getHits(int timestamp) {
while(!dq.empty() && timestamp - dq.front().first >= 300) {
cnt -= dq.front().second;
dq.pop_front();
}
return cnt;
}
private:
int cnt = 0;
deque<pair<int, int>> dq;
};
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter* obj = new HitCounter();
* obj->hit(timestamp);
* int param_2 = obj->getHits(timestamp);
*/