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.

class HitCounter {

    Queue<Integer> queue;
/** Initialize your data structure here. */
public HitCounter() {
queue = new LinkedList<>();
} /** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
queue.offer(timestamp);
} /** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
while(!queue.isEmpty() && timestamp - queue.peek() >= 300) {
queue.poll();
}
return queue.size();
}
} /**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/

最新文章

  1. 解决poshytip 表单高度大于屏幕高端 显示问题
  2. Python之路,Day2 - Python基础2
  3. 运动规划 (Motion Planning): MoveIt! 与 OMPL
  4. LEETCODE —— Surrounded Regions
  5. myBatis,Spring,SpringMVC三大框架ssm整合模板
  6. 为你的网页图标(Favicon)添加炫丽的动画和图片
  7. Android中获取正在运行的应用程序-----ActivityManager.RunningAppProcessInfo类详解
  8. ASP.NET MVC3调用分部视图-PartialView的几种方式(集)
  9. DX11 Without DirectX SDK--05 键盘和鼠标输入
  10. JVM-String.intern()
  11. JSON数据写入和解析
  12. Problem E: 平面上的点——Point类 (V)
  13. Java基础编程题——素数
  14. 总结: 在fc23中, 安装音频mp3 视频flv 的播放插件其实很简单, 只要一步就可以了: dnf install gstreamer1-libav
  15. 糟糕的@@identity,SCOPE_IDENTITY ,IDENT_CURRENT
  16. 微信小程序之雪碧图(css script)
  17. wpf mvvm模式下的image绑定
  18. 嵌入式C语言自我修养 05:零长度数组
  19. 关于NHibernate的一些代码
  20. 大数据平台-修改主机名及ssh免密码登录

热门文章

  1. javaweb04 ServletRequest&amp;ServletResponse
  2. 运营商何时会取消40G或100G流量封顶呢?短期内有望实现吗?
  3. Dp(NOIp级)全解
  4. h5-钟表动画案例
  5. python中selenium自动化常用关键字
  6. C++代码质量度量工具大阅兵
  7. nvm安装教程
  8. c#学习笔记05——数组&amp;集合
  9. 运行SQL文件报错Invalid ON UPDATE clause for &#39;create_date&#39; column
  10. mysql group_concat和find_in_set的使用