-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
38 lines (33 loc) · 1012 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class HitCounter {
private Map<Integer, Integer> counter;
/** Initialize your data structure here. */
public HitCounter() {
counter = new HashMap<>();
}
/**
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
*/
public void hit(int timestamp) {
counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1);
}
/**
Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity).
*/
public int getHits(int timestamp) {
int hits = 0;
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
if (entry.getKey() + 300 > timestamp) {
hits += entry.getValue();
}
}
return hits;
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/