Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d36d06a

Browse files
design hit counter
1 parent cb7d5f4 commit d36d06a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package medium;
2+
3+
public class DesignHitCounter {
4+
5+
}
6+
7+
class HitCounter {
8+
/**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,
9+
* I added one more field k to make it more generic.*/
10+
private int[] times;
11+
private int[] hits;
12+
private int k;
13+
14+
/** Initialize your data structure here. */
15+
public HitCounter() {
16+
k = 300;
17+
times = new int[k];
18+
hits = new int[k];
19+
}
20+
21+
/** Record a hit.
22+
@param timestamp - The current timestamp (in seconds granularity). */
23+
public void hit(int timestamp) {
24+
int index = timestamp % k;
25+
if (times[index] != timestamp){
26+
times[index] = timestamp;
27+
hits[index] = 1;
28+
} else {
29+
hits[index]++;
30+
}
31+
}
32+
33+
/** Return the number of hits in the past 5 minutes.
34+
@param timestamp - The current timestamp (in seconds granularity). */
35+
public int getHits(int timestamp) {
36+
int total = 0;
37+
for (int i = 0; i < k; i++){
38+
if (timestamp - times[i] < k){
39+
total += hits[i];
40+
}
41+
}
42+
return total;
43+
}
44+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
|370|[Range Addition](https://leetcode.com/problems/range-addition/)|[Solution](../../blob/master/MEDIUM/src/medium/RangeAddition.java)| O(n+k)|O(1) | Medium|
3535
|366|[Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/)|[Solution](../../blob/master/MEDIUM/src/medium/FindLeavesofBinaryTree.java)| O(n)|O(h) | Medium| DFS
3636
|364|[Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/)|[Solution](../../blob/master/MEDIUM/src/medium/NestedListWeightSumII.java)| O(n)|O(h) | Medium| DFS
37+
|362|[Design Hit Counter](https://leetcode.com/problems/design-hit-counter/)|[Solution](../../blob/master/MEDIUM/src/medium/DesignHitCounter.java)| O(1) amortized|O(k) | Medium| Design
3738
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../../blob/master/EASY/src/easy/LoggerRateLimiter.java)| amortized O(1)|O(k) | Easy| HashMap
3839
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
3940
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search

0 commit comments

Comments
 (0)