首页 > 代码库 > Leetcode: Design Hit Counter
Leetcode: Design Hit Counter
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 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. It is possible that several hits arrive roughly at the same time. Example: HitCounter counter = new HitCounter(); // 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); // hit at timestamp 300. counter.hit(300); // get hits at timestamp 300, should return 4. counter.getHits(300); // get hits at timestamp 301, should return 3. counter.getHits(301); Follow up: What if the number of hits per second could be very large? Does your design scale?
Use Queue
1 public class HitCounter { 2 Queue<Integer> queue; 3 4 /** Initialize your data structure here. */ 5 public HitCounter() { 6 queue = new LinkedList<Integer>(); 7 } 8 9 /** Record a hit. 10 @param timestamp - The current timestamp (in seconds granularity). */ 11 public void hit(int timestamp) { 12 while (!queue.isEmpty() && queue.peek().intValue()+300<=timestamp) { 13 queue.poll(); 14 } 15 queue.offer(timestamp); 16 } 17 18 /** Return the number of hits in the past 5 minutes. 19 @param timestamp - The current timestamp (in seconds granularity). */ 20 public int getHits(int timestamp) { 21 while (!queue.isEmpty() && queue.peek().intValue()+300<=timestamp) { 22 queue.poll(); 23 } 24 return queue.size(); 25 } 26 } 27 28 /** 29 * Your HitCounter object will be instantiated and called as such: 30 * HitCounter obj = new HitCounter(); 31 * obj.hit(timestamp); 32 * int param_2 = obj.getHits(timestamp); 33 */
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 300 seconds. hit[] array is wrapped around by mod operation. Each hit bucket is associated with times[] bucket which record current time. If it is not current time, it means it is at least 300s or 600 or even longer time ago and need to reset to 1.
1 public class HitCounter { 2 int[] time; 3 int[] hits; 4 5 /** Initialize your data structure here. */ 6 public HitCounter() { 7 time = new int[300]; 8 hits = new int[300]; 9 } 10 11 /** Record a hit. 12 @param timestamp - The current timestamp (in seconds granularity). */ 13 public void hit(int timestamp) { 14 if (time[timestamp%300] != timestamp) { 15 time[timestamp%300] = timestamp; 16 hits[timestamp%300] = 1; 17 } 18 else hits[timestamp%300]++; 19 } 20 21 /** Return the number of hits in the past 5 minutes. 22 @param timestamp - The current timestamp (in seconds granularity). */ 23 public int getHits(int timestamp) { 24 int res = 0; 25 for (int i=0; i<300; i++) { 26 if (time[i]+300 > timestamp) { 27 res += hits[i]; 28 } 29 } 30 return res; 31 } 32 } 33 34 /** 35 * Your HitCounter object will be instantiated and called as such: 36 * HitCounter obj = new HitCounter(); 37 * obj.hit(timestamp); 38 * int param_2 = obj.getHits(timestamp); 39 */
Leetcode: Design Hit Counter
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。