forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignHitCounter.java
44 lines (38 loc) · 1.18 KB
/
DesignHitCounter.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
39
40
41
42
43
44
package medium;
public class DesignHitCounter {
}
class HitCounter {
/**Looked at this post: https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data-structure-is-needed,
* I added one more field k to make it more generic.*/
private int[] times;
private int[] hits;
private int k;
/** Initialize your data structure here. */
public HitCounter() {
k = 300;
times = new int[k];
hits = new int[k];
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
int index = timestamp % k;
if (times[index] != timestamp){
times[index] = timestamp;
hits[index] = 1;
} else {
hits[index]++;
}
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
int total = 0;
for (int i = 0; i < k; i++){
if (timestamp - times[i] < k){
total += hits[i];
}
}
return total;
}
}