Design a hit counter which counts the number of hits received in the past 5 minutes.
Each function accepts a timestamp parameter (in seconds granularity) and your may assume that calls is being made to the S Ystem in chronological order (ie, the timestamp is monotonically increasing). Assume the earliest timestamp starts at 1.
It is possible this several hits arrive roughly at the same time.
Example:
Follow up:
What if the number of hits per second could is very large? Does your design scale?
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
This problem lets us design a click Counter, can return the number of clicks within five minutes, suggesting that there may be multiple clicks in the same time. Because the operation is chronological, the next time stamp will be greater than the time stamp, then the most straightforward way is to use a queue, each click on the current timestamp is added to the queue, and then we need to get the number of clicks, we start from the beginning of the queue to see, If the start time stamp is 5 minutes away, delete it until the start timestamp stops within 5 minutes, and then return the number of elements in the queue as the number of clicks you are seeking, see the code below:
Solution One:
classHitCounter { Public: /** Initialize your data structure here.*/hitcounter () {}/** Record a hit. @param timestamp-the current timestamp (in seconds granularity). */ voidHitinttimestamp) {Q.push (timestamp); } /** Return the number of hits in the past 5 minutes. @param timestamp-the current timestamp (in seconds granularity). */ intGethits (inttimestamp) { while(!q.empty () && Timestamp-q.front () >= -) {q.pop (); } returnq.size (); }Private: Queue<int>q;};
The following method is similar to the above method, with an array to save all the time stamp, and then to return the number of hits, only need to find the first time stamp in 5 minutes from the beginning of the coordinates, and then the total length of the array minus this coordinate, and the above method is different, this method does not delete the previous timestamp, The disadvantage is that it takes up a lot of space and the lower the efficiency, see the following code:
Solution Two:
classHitCounter { Public: /** Initialize your data structure here.*/hitcounter () {}/** Record a hit. @param timestamp-the current timestamp (in seconds granularity). */ voidHitinttimestamp) {V.push_back (timestamp); } /** Return the number of hits in the past 5 minutes. @param timestamp-the current timestamp (in seconds granularity). */ intGethits (inttimestamp) { intI, J; for(i =0; I < v.size (); ++i) {if(V[i] > timestamp- -) { Break; } } returnV.size ()-i; }Private: Vector<int>v;};
As follow up says there will be a lot of clicks per second, the following method is more ingenious, the definition of two size of 300 one-dimensional array times and hits, respectively, to save the time stamp and the number of clicks, in the click Function, the timestamp to 300 to take the remainder, Then see if the timestamp in this position is the same as the current timestamp, the same timestamp, then the corresponding number of clicks is increased by 1, and if it is not the same, it has been five minutes, then resets the corresponding number of hits to 1. So when we return the number of hits, we need to traverse the Times array, find out all the locations within 5 points, and add up the number of clicks in the corresponding position in hits, see the code below:
Solution Three:
classHitCounter { Public: /** Initialize your data structure here.*/HitCounter () {times.resize ( -); Hits.resize ( -); } /** Record a hit. @param timestamp-the current timestamp (in seconds granularity). */ voidHitinttimestamp) { intIDX = timestamp% -; if(TIMES[IDX]! =timestamp) {Times[idx]=timestamp; HITS[IDX]=1; } Else { ++Hits[idx]; } } /** Return the number of hits in the past 5 minutes. @param timestamp-the current timestamp (in seconds granularity). */ intGethits (inttimestamp) { intres =0; for(inti =0; I < -; ++i) {if(Timestamp-times[i] < -) {res+=Hits[i]; } } returnRes; }Private: Vector<int>Times , hits;};
Similar topics:
Logger Rate Limiter
Resources:
Https://leetcode.com/discuss/109492/java-solution-easy-to-understand
Https://leetcode.com/discuss/109489/simple-java-solution-with-explanation
https://leetcode.com/discuss/109499/super-easy-design-hit-gethits-fancy-data-structure-is-needed
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Design hit Counter designed click Counter