• [LeetCode] 362. 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?

    敲击计数器。设计一个敲击计数器,使它可以统计在过去5分钟内被敲击次数。每个函数会接收一个时间戳参数(以秒为单位),你可以假设最早的时间戳从1开始,且都是按照时间顺序对系统进行调用(即时间戳是单调递增)。在同一时刻有可能会有多次敲击。

    两种思路。

    1. 用一个queue记录每次敲击的timestamp,当试图访问getHits函数的时候,查看queue头部的元素的timestamp和当前timestamp是否差值大于300,若大于则说明无效了,需要弹出queue中所有跟目前timestamp差值大于300的元素,queue的size则是过去五分钟内的敲击次数。

    2. 用两个数组times和hits,分别记录敲击的时间和次数。敲击的时候,判断timestamp是否在300秒开外了(通过%300的方式),若大于300秒则需要将hits同样位置的数字重置为1;若小于300秒则直接在hits同样位置++,说明这依然是五分钟以内的敲击,同时在times数组内记录最新的timestamp。统计敲击次数的时候,是遍历hits数组,若hits[i]不为0,则判断当前i位置上times和timestamp的差值是否又大于300,若大于则不能加入结果集。

    Java实现

     1 class HitCounter {
     2     private int[] times;
     3     private int[] hits;
     4 
     5     /** Initialize your data structure here. */
     6     public HitCounter() {
     7         times = 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         int index = timestamp % 300;
    15         if (times[index] != timestamp) {
    16             times[index] = timestamp;
    17             hits[index] = 1;
    18         } else {
    19             hits[index]++;
    20         }
    21     }
    22 
    23     /** Return the number of hits in the past 5 minutes.
    24         @param timestamp - The current timestamp (in seconds granularity). */
    25     public int getHits(int timestamp) {
    26         int total = 0;
    27         for (int i = 0; i < 300; i++) {
    28             if (timestamp - times[i] < 300) {
    29                 total += hits[i];
    30             }
    31         }
    32         return total;
    33     }
    34 }
    35 
    36 /**
    37  * Your HitCounter object will be instantiated and called as such:
    38  * HitCounter obj = new HitCounter();
    39  * obj.hit(timestamp);
    40  * int param_2 = obj.getHits(timestamp);
    41  */

    相关题目

    362. Design Hit Counter

    933. Number of Recent Calls

    LeetCode 题目总结

  • 相关阅读:
    vue获取客户端ip地址
    Webpack 4 : ERROR in Entry module not found: Error: Can't resolve './src'
    PageHelper使用以及PageInfo中分页对象的转化
    PyCharm创建Django项目并连接mysql数据库
    Java常用类介绍
    Mybatis中的Mapper.xml映射文件sql查询接收多个参数
    学会聆听……
    SpringSecurity学习
    自定义线程池配置类
    雅虎的工程师团队给出的35个web开发最佳实践
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12695473.html
Copyright © 2020-2023  润新知