Skip to content

Commit 121b291

Browse files
refactor 362
1 parent fe7cf9b commit 121b291

File tree

2 files changed

+85
-43
lines changed

2 files changed

+85
-43
lines changed
Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.fishercoder.solutions;
2-
/**Design a hit counter which counts the number of hits received in the past 5 minutes.
2+
/**
3+
* 362. Design Hit Counter
4+
*
5+
* Design a hit counter which counts the number of hits received in the past 5 minutes.
36
4-
Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1.
7+
Each function accepts a timestamp parameter (in seconds granularity) and you may assume
8+
that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing).
9+
You may assume that the earliest timestamp starts at 1.
510
611
It is possible that several hits arrive roughly at the same time.
712
@@ -29,55 +34,60 @@ Each function accepts a timestamp parameter (in seconds granularity) and you may
2934
// get hits at timestamp 301, should return 3.
3035
counter.getHits(301);
3136
Follow up:
32-
What if the number of hits per second could be very large? Does your design scale?*/
37+
What if the number of hits per second could be very large? Does your design scale?
38+
*/
39+
3340
public class _362 {
3441

35-
class HitCounter {
36-
/**
37-
* 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,
38-
* I added one more field k to make it more generic.
39-
*/
40-
private int[] times;
41-
private int[] hits;
42-
private int k;
42+
public static class Solution1 {
43+
public static class HitCounter {
44+
/**
45+
* Reference: https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data-structure-is-needed,
46+
* I added one more field k to make it more generic.
47+
* It basically maintains a window of size 300, use modular to update the index.
48+
*/
49+
private int[] times;
50+
private int[] hits;
51+
private int k;
4352

44-
/**
45-
* Initialize your data structure here.
46-
*/
47-
public HitCounter() {
48-
k = 300;
49-
times = new int[k];
50-
hits = new int[k];
51-
}
53+
/**
54+
* Initialize your data structure here.
55+
*/
56+
public HitCounter() {
57+
k = 300;
58+
times = new int[k];
59+
hits = new int[k];
60+
}
5261

53-
/**
54-
* Record a hit.
55-
*
56-
* @param timestamp - The current timestamp (in seconds granularity).
57-
*/
58-
public void hit(int timestamp) {
59-
int index = timestamp % k;
60-
if (times[index] != timestamp) {
61-
times[index] = timestamp;
62-
hits[index] = 1;
63-
} else {
64-
hits[index]++;
62+
/**
63+
* Record a hit.
64+
*
65+
* @param timestamp - The current timestamp (in seconds granularity).
66+
*/
67+
public void hit(int timestamp) {
68+
int index = timestamp % k;
69+
if (times[index] != timestamp) {
70+
times[index] = timestamp;
71+
hits[index] = 1;
72+
} else {
73+
hits[index]++;
74+
}
6575
}
66-
}
6776

68-
/**
69-
* Return the number of hits in the past 5 minutes.
70-
*
71-
* @param timestamp - The current timestamp (in seconds granularity).
72-
*/
73-
public int getHits(int timestamp) {
74-
int total = 0;
75-
for (int i = 0; i < k; i++) {
76-
if (timestamp - times[i] < k) {
77-
total += hits[i];
77+
/**
78+
* Return the number of hits in the past 5 minutes.
79+
*
80+
* @param timestamp - The current timestamp (in seconds granularity).
81+
*/
82+
public int getHits(int timestamp) {
83+
int total = 0;
84+
for (int i = 0; i < k; i++) {
85+
if (timestamp - times[i] < k) {
86+
total += hits[i];
87+
}
7888
}
89+
return total;
7990
}
80-
return total;
8191
}
8292
}
8393
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._362;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _362Test {
10+
private static _362.Solution1.HitCounter hitCounter;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
hitCounter = new _362.Solution1.HitCounter();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
hitCounter.hit(1);
20+
hitCounter.hit(2);
21+
hitCounter.hit(3);
22+
assertEquals(3, hitCounter.getHits(4));
23+
24+
hitCounter.hit(300);
25+
assertEquals(4, hitCounter.getHits(300));
26+
assertEquals(3, hitCounter.getHits(301));
27+
28+
hitCounter.hit(301);
29+
assertEquals(4, hitCounter.getHits(300));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy