Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd36d06a

Browse files
design hit counter
1 parentcb7d5f4 commitd36d06a

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+
packagemedium;
2+
3+
publicclassDesignHitCounter {
4+
5+
}
6+
7+
classHitCounter {
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+
privateint[]times;
11+
privateint[]hits;
12+
privateintk;
13+
14+
/** Initialize your data structure here. */
15+
publicHitCounter() {
16+
k =300;
17+
times =newint[k];
18+
hits =newint[k];
19+
}
20+
21+
/** Record a hit.
22+
@param timestamp - The current timestamp (in seconds granularity). */
23+
publicvoidhit(inttimestamp) {
24+
intindex =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+
publicintgetHits(inttimestamp) {
36+
inttotal =0;
37+
for (inti =0;i <k;i++){
38+
if (timestamp -times[i] <k){
39+
total +=hits[i];
40+
}
41+
}
42+
returntotal;
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)

[8]ページ先頭

©2009-2025 Movatter.jp