Design a hit counter which counts the number of hits received in the past 5minutes. 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 at1. It is possible this several hits arrive roughly at the same time. Example:hitcounter counter=Newhitcounter ();//Hit at timestamp 1.Counter.hit (1);//Hit at timestamp 2.Counter.hit (2);//Hit at timestamp 3.Counter.hit (3);//get hits at timestamp 4, should return 3.Counter.gethits (4);//Hits at timestamp.Counter.hit (300);//get hits at timestamp, should return 4.Counter.gethits (300);//get hits at timestamp 301, should return 3.Counter.gethits (301); Follow Up:whatifThe number of hits per second could be very large? Does your design scale?
Use Queue
1 Public classHitCounter {2Queue<integer>queue;3 4 /**Initialize your data structure here.*/5 PublicHitCounter () {6Queue =NewLinkedlist<integer>();7 }8 9 /**Record a hit.Ten @paramtimestamp-the current timestamp (in seconds granularity).*/ One Public voidHitinttimestamp) { A while(!queue.isempty () && Queue.peek (). Intvalue () +300<=timestamp) { - Queue.poll (); - } the Queue.offer (timestamp); - } - - /**Return the number of hits in the past 5 minutes. + @paramtimestamp-the current timestamp (in seconds granularity).*/ - Public intGethits (inttimestamp) { + while(!queue.isempty () && Queue.peek (). Intvalue () +300<=timestamp) { A Queue.poll (); at } - returnqueue.size (); - } - } - - /** in * Your HitCounter object would be instantiated and called as such: - * HitCounter obj = new HitCounter (); to * Obj.hit (timestamp); + * int param_2 = obj.gethits (timestamp); - */
Better Solution:can solve follow up, refer to https://discuss.leetcode.com/topic/48758/ super-easy-design-o-1-hit-o-s-gethits-no-fancy-data-structure-is-needed
Basic ideal is using buckets. 1 bucket for every second because we only need to keep the recent hits info for seconds. Hit[] Array is wrapped around by mod operation. Each of the hit buckets are associated with times[] buckets which record current time. If It isn't current time, it means it was at least 300s or even longer time ago and need to reset to 1.
1 Public classHitCounter {2 int[] time;3 int[] hits;4 5 /**Initialize your data structure here.*/6 PublicHitCounter () {7Time =New int[300];8hits =New int[300];9 }Ten One /**Record a hit. A @paramtimestamp-the current timestamp (in seconds granularity).*/ - Public voidHitinttimestamp) { - if(time[timestamp%300]! =timestamp) { theTIME[TIMESTAMP%300] =timestamp; -HITS[TIMESTAMP%300] = 1; - } - Elsehits[timestamp%300]++; + } - + /**Return the number of hits in the past 5 minutes. A @paramtimestamp-the current timestamp (in seconds granularity).*/ at Public intGethits (inttimestamp) { - intres = 0; - for(inti=0; i<300; i++) { - if(Time[i]+300 >timestamp) { -Res + =Hits[i]; - } in } - returnRes; to } + } - the /** * * Your HitCounter object would be instantiated and called as such: $ * HitCounter obj = new HitCounter ();Panax Notoginseng * Obj.hit (timestamp); - * int param_2 = obj.gethits (timestamp); the */
Leetcode:design hit Counter