博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-Design Hit Counter
阅读量:4665 次
发布时间:2019-06-09

本文共 2936 字,大约阅读时间需要 9 分钟。

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?

Credits:

Special thanks to for adding this problem and creating all test cases.

Analysis:

Since only recording 300 second, we can directly use CircularBuffer.

Solution:

1 public class HitCounter { 2         /** Initialize your data structure here. */ 3         public HitCounter() { 4             hit = new int[300]; 5             head = hit.length - 1; 6             lastCallTime = 0; 7             totalHits = 0; 8         } 9 10         public void advanceTime(int timestamp) {11             int duration = timestamp - lastCallTime;12             lastCallTime = timestamp;13             14             if (duration>=300){15                 Arrays.fill(hit,0);16                 totalHits = 0;17                 head = (head+duration) % hit.length;18                 19                 return;20             }21             22             for (int i = 0; i < duration; i++) {23                 head = (head + 1) % hit.length;24                 totalHits -= hit[head];25                 hit[head] = 0;26             }27         }28 29         /**30          * Record a hit.31          * 32          * @param timestamp33          *            - The current timestamp (in seconds granularity).34          */35         public void hit(int timestamp) {36             advanceTime(timestamp);37             hit[head]++;38             totalHits++;39         }40 41         /**42          * Return the number of hits in the past 5 minutes.43          * 44          * @param timestamp45          *            - The current timestamp (in seconds granularity).46          */47         public int getHits(int timestamp) {48             advanceTime(timestamp);49             return totalHits;50         }51 52         int[] hit;53         int head;54         int lastCallTime;55         int totalHits;56 }57 58 /**59  * Your HitCounter object will be instantiated and called as such:60  * HitCounter obj = new HitCounter();61  * obj.hit(timestamp);62  * int param_2 = obj.getHits(timestamp);63  */

 

转载于:https://www.cnblogs.com/lishiblog/p/5691644.html

你可能感兴趣的文章
Cocos2d-x 3.2 学习笔记(七)Scene And Transition
查看>>
Re:JavaScript
查看>>
ajax 200 4 parseerror的一个问题
查看>>
面试题之实现系统函数系列一:实现memmove函数
查看>>
数据结构:可持久化并查集
查看>>
基于UDP协议的Socket通信
查看>>
linux安装配置Redis,Swoole扩展
查看>>
C语言-02基本运算
查看>>
轻巧快速的JSON工具--fastJSON
查看>>
生男生女预测软件,千人验证无误
查看>>
js call()和apply()方法的区别和用法详解
查看>>
Android之Service
查看>>
HDU 2795 Billboard 解题报告
查看>>
多线程——newCachedThreadPool线程池
查看>>
日志传输与清除脚本
查看>>
maven小知识点
查看>>
flash bulider 生成app无法安装在xcode模拟器上
查看>>
路由器中的PPP配置与DHCP服务器配置
查看>>
hdu3436 splaytree树模拟队列+离散化缩点
查看>>
2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
查看>>