zoukankan      html  css  js  c++  java
  • 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  */
  • 相关阅读:
    在WinForm应用程序中快速实现多语言的处理
    使用EasyNetQ组件操作RabbitMQ消息队列服务
    在GridControl表格控件中实现多层级主从表数据的展示
    在Winform混合式框架中整合外部API接口的调用
    快看Sample代码,速学Swift语言(3)-运算符
    快看Sample代码,速学Swift语言(1)-语法速览
    基于信封套打以及批量打印的实现过程
    Winform界面中实现通用工具栏按钮的事件处理
    Winform界面中实现菜单列表的动态个性化配置管理
    双指针模板
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6193707.html
Copyright © 2011-2022 走看看