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  */
  • 相关阅读:
    分布式系统基础设施
    6种负载均衡算法
    缓存类
    HTTP状态码100、200、300、400、500、600的含义
    Java中的多线程你只要看这一篇就够了(引用)
    An incompatible version 1.1.1 of the APR based Apache Tomcat Native library is installed, while Tomcat requires version 1.1.17
    EL表达式与JSTL(C)标签
    JSP标准标签库
    SpringMVC HttpMessageConverter 匹配规则
    springboot学习(三)——使用HttpMessageConverter进行http序列化和反序列化
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6193707.html
Copyright © 2011-2022 走看看