-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHitCounter.java
More file actions
64 lines (47 loc) · 1.63 KB
/
Copy pathHitCounter.java
File metadata and controls
64 lines (47 loc) · 1.63 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.HashMap;
import java.io.*;
import java.util.*;
class HitCounter {
HashMap<Integer, Integer> hits_so_far;
ArrayList<Integer> hit_time;
int total_hits;
/** Initialize your data structure here. */
public HitCounter() {
this.hits_so_far = new HashMap<>();
this.hit_time = new ArrayList<>();
this.total_hits = 0;
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int time) {
//this.hits.put(time, this.hits.getOrDefault(time, 0)+1);
this.total_hits++;
this.hits_so_far.put(time, this.total_hits);
int n = this.hit_time.size();
if (n==0 || this.hit_time.get(n-1) != time)
this.hit_time.add(time);
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int time) {
if (time<=300)
return this.total_hits;
int i = this.hit_time.size()-1;
int res = this.total_hits;
while (i>=0 && (time-this.hit_time.get(i)) < 300 ){
i -= 1;
}
if (i<0)
return res;
else{
int prev_time = this.hit_time.get(i);
return res - this.hits_so_far.get(prev_time);
}
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/