-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek 7 362. Design Hit Counter
41 lines (37 loc) · 1.07 KB
/
Week 7 362. Design Hit Counter
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
public class HitCounter {
int[] times;
int[] hits;
/** Initialize your data structure here. */
public HitCounter() {
times = new int [300];
hits = new int [300];
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit( int timestamp ) {
int index = timestamp % 300;
if (times[index] != timestamp){
times[index] = temstamp;
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 < 300; i++){
if (timestamp - time[i] < 300){
total += hits[1];
}
}
return total;
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/